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

114 views Asked by At

There are two ending days like beloew

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

I am getting the month, day and year by following

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];

This is what i am getting for 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

For B, I am getting

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

I am confusing why 2029-12-31T00:00:00+00:00 just give me a day off by 1. Any idea about this ?

1

There are 1 answers

2
rmaddy On BEST ANSWER

It's due the difference between your timezone and UTC. The value 2029-12-31T00:00:00+00:00 is on December 31 in the UTC timezone. But when you setup the date components you are getting back the date in your timezone where it's December 30 local time.