我需要一个指向 UTC 上周日中午的日期戳。我从上周日凌晨 12:00 动态创建日期得到了一些非常接近的结果 :
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:1]; //Sunday
[nowComponents setHour:12]; // noon
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *previousSunday = [calendar dateFromComponents:nowComponents];
但这给了我NSDate
“当前时区中午”的对象,它通常与“GTM/UTC 中午”不匹配。如何更正此日期以考虑时区?