繁体   English   中英

NSDateComponents将UTC日期转换为本地时区,弄乱了“今天”的nsdate比较

[英]NSDateComponents converting UTC date to local timezone, messing up “today” nsdate comparison

我正在尝试使用NSDateComponents来测试动态日期是否发生在“今天”。 我用它来提取年/月/日,然后比较两个NSDate的值。 从理论上讲,这应该起作用。 我在东海岸,所以EST,UTC的时间是-5。

当前实际时间:美国东部标准时间12:40:53

这是UTC中正确的NSDate。 时间是正确的UTC:

  • 当前[NSD发布日期]:2016-01-19 17:40:53 +0000
  • 活动NSD发布日期:2016-01-19 00:00:00 +0000

这是我的日志,显示每个日期。 它将日期转换为本地时区2016-01-19 07:00:00。 由于当前日期是一天中的某天,因此-5小时的偏移量不会导致其偏移日期。 然而,午夜时分又回到了一天。 我如何才能尊重UTC?

NSLog的:

2016-1-19 12:40:53 (America/New_York (EST) offset -18000), 
2016-1-18 19:0:0 (America/New_York (EST) offset -18000)

码:

NSDateComponents *current = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:_date];
NSDateComponents *activity = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:activityModel.date];

// attempt to set timezones to UTC manually, doesn't change anything   
activity.calendar = [NSCalendar currentCalendar];
activity.calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

current.calendar = [NSCalendar currentCalendar];
current.calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];


NSLog(@"%ld-%ld-%ld %ld:%ld:%ld (%@), %ld-%ld-%ld %ld:%ld:%ld (%@)", (long)[current year],(long)[current month], (long)[current day], (long)[current hour], (long)[current minute], (long)[current second], [current timeZone].description, (long)[activity year], (long)[activity month], (long)[activity day],(long)[activity hour], (long)[activity minute], (long)[activity second],[current timeZone].description);

if([current day] == [activity day] &&
           [current month] == [activity month] &&
           [current year] == [activity year] &&
           [current era] == [activity era]) {

           // do something if activity on current day
}

您正在更改NSDateComponents的时区。 但是您想在调用components (或调用NSCalendar日检查例程)之前更改日历的时区,例如:

NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

if ([calendar isDate:_date inSameDayAsDate:activityModel.date]) {
    NSLog(@"same");
} else {
    NSLog(@"diff");
}

但是,如果当地时间是2016-01-19 21:00:00 -0500月19日2016-01-19 21:00:00 -0500 (即世界标准时间20号) 2016-01-19 21:00:00 -0500 您是否想说示例中的活动日期也是同一天? 如果是这样,那么你真的说你要使用本地日历_date的,和UTC activityModel.date ,如:

NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *current = [calendar components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:_date];

calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

NSDateComponents *activity = [calendar components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:activityModel.date];

if ([current day] == [activity day] && [current month] == [activity month] && [current year] == [activity year] && [current era] == [activity era]) {
    NSLog(@"same");
} else {
    NSLog(@"diff");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM