解析日期并将休息日减 1,nsdate

parsing the date and getting the day off by 1, nsdate

下面有两个结束日

A = 2029-12-31T00:00:00+00:00
B = 2029-12-31T12:00:00+00:00

我通过关注

得到月、日和年
NSDateFormatter *endDateFormatter = [[NSDateFormatter alloc] init];
[endDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *endDate = [endDateFormatter dateFromString:self[@"license"][@"endDate"]];
NSDateComponents *endDatecomponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:endDate];

这就是我得到的 A

Printing description of endDate:
2029-12-31 00:00:00 +0000
Printing description of endDatecomponents:
<NSDateComponents: 0x79e069e0>
    Calendar Year: 2029
    Month: 12
    Leap month: no
    Day: 30

对于 B,我得到

2029-12-31 12:00:00 +0000
Printing description of endDatecomponents:
<NSDateComponents: 0x7be8eb60>
    Calendar Year: 2029
    Month: 12
    Leap month: no
    Day: 31

我很困惑为什么 2029-12-31T00:00:00+00:00 只给我 1 点假。 对此有什么想法吗?

这是由于您的时区和 UTC 之间的差异所致。值 2029-12-31T00:00:00+00:00 是 UTC 时区的 12 月 31 日。但是,当您设置日期组件时,您将返回当地时间 12 月 30 日所在时区的日期。