错误的日期从NSDateComponents返回

时间:2013-05-13 11:11:36

标签: iphone ios objective-c nsdate nsdatecomponents

为什么这给了我错误的约会?

 NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    [components setYear:2013];
    [components setMonth:06];
    [components setDay:28];
    [components setHour:5];
    [components setMinute:00];
    NSDate *startDate1 = [myCalendar dateFromComponents:components];
    NSLog(@"start date is %@",startDate1);

开始日期是2013-06-28 03:00:00 +0000

修改

我想做的是以下内容。我有一个开始和结束。

例如:

开始日期是2013-06-28 05:00

结束日期是:2013-06-29 04:59

然后我想检查当前日期是否在开始日期和结束日期之间。我正在使用以下内容。

NSComparisonResult result1 = [now compare:startDate1];
    NSComparisonResult result2 = [now compare:endDate1];

if (result2 == NSOrderedSame && result1 == NSOrderedSame ) {
        NSLOG(@"OKE!");
 }

3 个答案:

答案 0 :(得分:3)

可能日期是正确的,但你误解了日志:

记录日期始终在TZ +0000中完成。例如,如果您在中欧,您将拥有(预期?)日期2013-06-28 05:00:00 +0200,但日志将显示标准化日期2013-06-28 03:00:00 +0000。这是相同的日期和时间!它只是以不同的方式表达。

+++

如果您的组件也在TZ +0000中,则应设置日历的时区。

答案 1 :(得分:1)

您可以检查当前日期是否在两个日期之间,如下所示:

NSDate *now = [NSDate date];
BOOL betweenStartAndEnd = ([startDate compare:now] == NSOrderedAscending && [endDate compare:now] == NSOrderedDescending);

您的代码实际上会检查测试日期是否与开始日期和结束日期相同(NSOrderedSame)(当然不是)

result2 == NSOrderedSame && result1 == NSOrderedSame

请参阅我的扩展示例:

NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[components setYear:2013];
[components setMonth:05];
[components setDay:13];
[components setHour:18];
[components setMinute:00];
NSDate *startDate = [myCalendar dateFromComponents:components];
[components setDay:15];
NSDate *endDate = [myCalendar dateFromComponents:components];

NSDate *now = [NSDate date];
BOOL betweenStartAndEnd = ([startDate compare:now] == NSOrderedAscending && [endDate compare:now] == NSOrderedDescending);

NSLog(@"Date %@ %@ between %@ and %@", now, betweenStartAndEnd ? @"IS" : @"IS NOT", startDate, endDate);

将此打印到控制台:

Date 2013-05-13 15:44:06 +0000 IS NOT between 2013-05-13 16:00:00 +0000 and 2013-05-15 16:00:00 +0000

答案 2 :(得分:0)

那是因为您必须使用设备的时区打印日期,否则以UTC显示。

NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    [components setYear:2013];
    [components setMonth:06];
    [components setDay:28];
    [components setHour:5];
    [components setMinute:00];
    NSDate *startDate1 = [myCalendar dateFromComponents:components];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSLog(@"%@", [formatter stringFromDate:startDate1]);