【问题标题】:Fire localNotification exactly at 5PM IST在 IST 下午 5 点准确触发 localNotification
【发布时间】:2014-07-29 10:55:52
【问题描述】:

我正在使用下面的代码在 IST 下午 5 点准确地触发 localNotification。但是当记录fireDate时,它没有显示我想要的时间。我是不是哪里出错了?

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    //localNotification.fireDate = myNewDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    NSDate *currentDate = [NSDate date];
    NSDate *fireDate = nil;

    //[dateComponents setDay:3];  // ...or whatever day.
    [dateComponents setHour:11];
    [dateComponents setMinute:30];

    fireDate = [calendar dateByAddingComponents:dateComponents
                                          toDate:currentDate
                                         options:0];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    NSString *stringFromDate = [formatter stringFromDate:fireDate];


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:stringFromDate];

    NSLog(@"Fire date : %@",dateFromString);
    [localNotification setFireDate:dateFromString];

我的输出: 发布日期2014-07-29 16:48:20 +0000

【问题讨论】:

  • 目标是什么——“今天下午 5 点 IST”? “下次是 IST 下午 5 点,即使那是明天”?还有什么?

标签: ios7 nsdateformatter uilocalnotification nscalendar nstimezone


【解决方案1】:

您没有得到下午 5 点的原因是代码中没有尝试将时间设置为下午 5 点的内容。相反,你有这个:

[dateComponents setHour:11];
[dateComponents setMinute:30];

fireDate = [calendar dateByAddingComponents:dateComponents
                                      toDate:currentDate
                                     options:0];

你得到的是未来 11 小时 30 分钟的时间。除非您恰好在凌晨 5:30 运行代码,否则您不会得到下午 5 点。但是代码没有在任何地方提到IST,所以你不会在那个区域得到下午5点。

您在代码 sn-p 末尾所做的事情也令人困惑——将 fireDate 转换为字符串,然后将该字符串转换回日期。这没有任何意义。

要在特定时区获得下午 5 点,您需要执行以下操作。这会在请求的时区获得下一个下午 5 点,如果那里已经是下午 5 点,则可能是明天:

    NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setTimeZone:zone];

    NSInteger targetHour = 17; // 5pm

    NSDate *now = [NSDate date];
    NSInteger componentFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit;
    NSDateComponents *nowDateComponents = [calendar components:componentFlags fromDate:now];

    if (nowDateComponents.hour >= targetHour) {
        nowDateComponents.day += 1;
    }
    nowDateComponents.hour = targetHour;

    NSDate *targetDate = [calendar dateFromComponents:nowDateComponents];

如果您想以人类可读的形式打印它,您可以执行以下操作。但NSDateFormatter 仅用于向用户显示日期 - 它不是获取所需NSDate 的一部分。

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:zone];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterLongStyle];
    NSString *targetDateString = [formatter stringFromDate:targetDate];
    NSLog(@"Target date: %@", targetDateString);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2019-02-11
    • 2014-06-08
    • 2014-09-27
    • 2010-09-17
    相关资源
    最近更新 更多