【问题标题】:NSTimeInterval of startOfDay NSDate to GMTstartOfDay NSDate 到 GMT 的 NSTimeInterval
【发布时间】:2013-08-05 12:27:38
【问题描述】:

我试图将时间设置为零的 NSDate 的 NSTimeInterval 设置为一天的开始,但始终给我我的时区是 GMT+2 而不是 GMT,我正在这样做:

这是在 NSDate 中设置一天开始的类别:

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]

- (NSDate *) dateAtStartOfDay
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
components.hour = 0;
components.minute = 0;
components.second = 0;
return [CURRENT_CALENDAR dateFromComponents:components];
}

然后我这样做:

NSTimeInterval now = [[[NSDate date] dateAtStartOfDay] timeIntervalSince1970];

并给我这个 NSTimeInterval:

1375567200

如果我检查:

http://www.epochconverter.com

格林威治时间是这样的:

GMT: Sat, 03 Aug 2013 22:00:00 GMT

Mine tiem zone: 04 agosto 2013 00:00:00 CEST GMT+2

我如何才能始终拥有 GMT 时间间隔?我正在寻找一种适用于纽约、罗马、里约热内卢的解决方案,例如,必须给我 nstimeinterval 到 gmt 用户在哪里,任何人都可以帮助我吗?

【问题讨论】:

    标签: objective-c nsdate nstimeinterval nstimezone


    【解决方案1】:

    您从currentCalendar 获得的日历使用当前区域设置,因此使用运行该系统的任何系统的时区。您应该为此计算创建自己的日历,将其时区设置为 GMT。

    NSCalendar * GMTCal = [[NSCalendar alloc] initWithCalendarIdentifier:[[NSCalendar currentCalendar] calendarIdentifier];
    [GMTCal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    

    【讨论】:

      猜你喜欢
      • 2011-12-19
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2011-08-02
      • 2011-07-31
      • 2010-12-24
      相关资源
      最近更新 更多