我正在尝试将时间设置为零的NSTimeInterval的NSDate,所以一天的开始,但总是给我的时区,即GMT+2,而不是格林尼治时间,我这样做:
这是在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我怎么能总是有格林尼治时间间隔呢?我正在寻找一个解决方案,在纽约,罗马,里约热内卢,例如,必须给我的时间间隔到格林尼治标准时间,无论用户在哪里,谁可以帮助我?
发布于 2013-08-04 20:36:47
从currentCalendar获得的日历使用当前的区域设置,从而使用运行在任何系统上的时区。您应该为此计算创建自己的日历,将其时区设置为格林尼治标准时间。
NSCalendar * GMTCal = [[NSCalendar alloc] initWithCalendarIdentifier:[[NSCalendar currentCalendar] calendarIdentifier];
[GMTCal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];https://stackoverflow.com/questions/18047304
复制相似问题