NSDate转换NSDateComponents问题

时间:2013-04-24 10:29:03

标签: ios objective-c nsdatecomponents

destinationDate和组件的结果不同。 我该如何解决这个问题?

NSTimeZone* sourceTimeZone      = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSDate* sourceDate              = [NSDate date];
NSInteger sourceGMTOffset       = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset  = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval         = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate         = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

NSCalendar *calendar            = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components    = [calendar components:(NSYearCalendarUnit  |
                                                        NSMonthCalendarUnit |
                                                        NSDayCalendarUnit   |
                                                        NSHourCalendarUnit  |
                                                        NSMinuteCalendarUnit|
                                                        NSSecondCalendarUnit) fromDate:destinationDate];

NSLog(@"%@", destinationDate); 
NSLog(@"%@", components);

2013-04-24 19:27:09 +0000

    日历年:2013年     月:4     闰月:没有     第25天     小时:4     分钟:27     第二:9

2 个答案:

答案 0 :(得分:3)

日期(destinationDate)以GMT格式打印。但是,日期组件不是因为日历是使用本地时区创建的。

如果您使用calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]进行尝试,您将从两者获得相同的结果。否则,您将获得两次差异(第一次由您的代码添加,第二次由日历添加)

实际上,你的代码没有多大意义。永远不应该按时区修改NSDate,因为它不包含任何时区信息。 NSDate始终代表GMT中的日期。仅当您想要将日期转换为字符串时才转换日期 - 当您这样做时,请使用NSCalendarNSDateFormatter并设置正确的时区。

以下代码足以使用系统(默认)时区打印出日期

NSDate* sourceDate              = [NSDate date];
NSCalendar *calendar            = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [calendar components:(NSYearCalendarUnit  |
                                                     NSMonthCalendarUnit |
                                                     NSDayCalendarUnit   |
                                                     NSHourCalendarUnit  |
                                                     NSMinuteCalendarUnit|
                                                     NSSecondCalendarUnit) fromDate:destinationDate];

NSLog(@"%@", sourceDate); 
NSLog(@"%@", components);

答案 1 :(得分:1)

NSDateComponents总是以GMT的形式提供日期,所以只需在下方放置即可获得当前日期。

NSDateComponents *components    = [calendar components:(NSYearCalendarUnit  |
                                                            NSMonthCalendarUnit |
                                                            NSDayCalendarUnit   |
                                                            NSHourCalendarUnit  |
                                                            NSMinuteCalendarUnit|
                                                            NSSecondCalendarUnit) fromDate:[NSDate date]];

destinationDate替换为[NSDate date]

输出:

 2013-04-24 16:05:08 +0000

<NSDateComponents: 0x7e3e5b0>
    Calendar Year: 2013
    Month: 4
    Day: 24
    Hour: 16
    Minute: 5
    Second: 8