【问题标题】:How to convert a string Date & Time to NSDate in iOS? [duplicate]如何在 iOS 中将字符串日期和时间转换为 NSDate? [复制]
【发布时间】:2015-07-14 11:05:11
【问题描述】:

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

我有一个开始日期作为字符串。喜欢: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 小时格式,而这个日期格式也是 24 小时格式。

我的问题是通知没有触发。请帮帮我。

【问题讨论】:

  • 关于这两个主题有很多问题(例如stackoverflow.com/questions/20949897/…),你应该使用NSDateFormatter developer.apple.com/library/mac/documentation/Cocoa/Reference/…
  • 您的日期格式是“dd-MM-yy HH:mm”,那么为什么输出为“2015-07-15 11:01:00 +0000”
  • 是这样的。 :)
  • 如果您在控制台中遇到任何权限错误,您还必须添加以下代码。 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]]; }
  • 我已经做到了。通知未触发..

标签: ios datepicker uilocalnotification


【解决方案1】:

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

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 如何将此字符串转换为 NSDate & time ?

    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。您可以按照第一个问题的答案将字符串日期转换为 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];
    

    我现在刚刚尝试了这段代码,我收到了通知。

    【讨论】:

    • 不,这没有显示正确的日期。如何结合我的 2 日期和时间和日程通知?
    • @SRNayak :您必须根据需要设置日期格式。
    • 是的,我已经完成了,但没有触发通知。我认为日期格式是错误的。告诉我一件事,通知只需要 12 小时格式,还是 24 小时格式?
    • 如果您的设备格式是 24 小时制,您必须将触发日期设置为 24 小时制。否则,您可以使用 12 小时格式。
    • 如果我让它 12 小时 tehn 它会立即触发。没有等待预定的时间..
    【解决方案3】:

    从字符串中获取日期使用 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"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-06
      • 2018-03-24
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2016-03-08
      • 2019-12-15
      相关资源
      最近更新 更多