如何转换字符串Date&是时候在iOS中使用NSDate了吗?

时间:2015-07-14 11:05:12

标签: ios datepicker uilocalnotification

我在过去两天遇到了一个问题。我的要求是这样的。

我的开始日期为字符串。例如: start_Date == 15-07-15

结束日期为字符串。喜欢: end_Date == 16-07-15

时间也像:开始时间== 16:28,结束时间== 18:28

我合并了2个字符串并将其转换为NSDate

   NSString *strt_Date = [NSString stringWithFormat:@"%@ %@",start_Date, start_Time];
   NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
  [dateFormat setDateFormat:@"dd-MM-yy HH:mm"];
   NSDate *date = [dateFormat dateFromString:strt_Date];

输出是这样的:

 date == 2015-07-15 11:01:00 +0000

然后我发出通知:

[self scheduleNotificationForDate:date];

- (void)scheduleNotificationForDate:(NSDate *)date
{
  // Here we cancel all previously scheduled notifications
  [[UIApplication sharedApplication] cancelAllLocalNotifications];

  NSLog(@"date == %@",date);

  UILocalNotification *localNotif = [[UILocalNotification alloc] init];
  if (localNotif == nil)
      return;
  localNotif.fireDate = date;// this sets when notification will be called.
  // Notification details
  localNotif.timeZone = [NSTimeZone defaultTimeZone];
  localNotif.alertBody = @"Time to read your course";// this shows the alert box with message.
  // Set the action button

  localNotif.alertAction = @"View";
  localNotif.soundName = UILocalNotificationDefaultSoundName;
  localNotif.applicationIconBadgeNumber = -1;

  // Schedule the notification
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

注意:我的设备也是24 HR格式,此日期格式也是24小时格式。

我的问题是通知没有解雇。请帮助我。

3 个答案:

答案 0 :(得分:0)

1如何将此字符串转换为NSDate&时间?

NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];

2如何使用此日期和时间设置UILocalNotification。

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = selectedDate;
localNotif.alertBody = [NSString stringWithFormat:@"Notification for time : %@",newDate];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

这里'selectedDate'是一个NSDate。您可以按照第1个问题的答案将字符串日期转换为NSDate。

您的功能示例。

NSString *dateString = @"2015-07-15 12:19";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[self scheduleNotificationForDate:dateFromString];

我刚刚尝试了这段代码并收到了通知。

答案 1 :(得分:0)

首先从日期和时间创建单个字符串时间。

NSString *strt_Date = [NSString stringWithFormat:@"%@ %@",start_date,start_time];
NSString *endd_Date = [NSString stringWithFormat:@"%@ %@",end_date,end_time];

将此字符串传递给以下函数

-(NSDate*)dateFromString:(NSString*)strDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"dd-MM-yy HH:mm";

    return [dateFormatter dateFromString:strDate];
}

然后将其设置为fireDate

答案 2 :(得分:-1)

从字符串中获取日期使用NSDateFormatter ...比如

NSDateFormatter *formater = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formater setLocale:usLocale];
formater.dateStyle = NSDateIntervalFormatterFullStyle;
formater.dateFormat = @"yyyy-MM-dd";

NSDate *yourDate = [formater dateFromString:@"Your string"];
相关问题