NSCalendar creates wrong NSDate

Hi,
I was created the NSDate using NSCalendar with local time zone(Sydney/Australia), but it returns wrong date

NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:3];
    [comps setMonth:10];
    [comps setYear:2019];
    [comps setHour:23];
    [comps setMinute:00];
    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
    
    NSCalendar *calendar =[NSCalendar currentCalendar];
    calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:([[NSTimeZone localTimeZone] secondsFromGMT])];
    NSDate *date1 = [calendar dateFromComponents: comps];
    
    NSCalendar *geoCalender = [[NSCalendar alloc]initWithCalendarIdentifier:@"gregorian"];
    geoCalender.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:([[NSTimeZone localTimeZone] secondsFromGMT])];
    NSDate *geoDate = [geoCalender dateFromComponents: comps];
    
    NSLog(@"date: %@", date);
    NSLog(@"date: %@", date1);
    NSLog(@"date: %@", geoDate);
    
    NSDateComponents *comps1 = [[NSDateComponents alloc] init];
    [comps1 setDay:7];
    [comps1 setMonth:10];
    [comps1 setYear:2019];
    [comps1 setHour:23];
    [comps1 setMinute:00];
    NSDate *date2 = [[NSCalendar currentCalendar] dateFromComponents:comps1];
    NSDate *date3 = [calendar dateFromComponents: comps1];
    NSDate *geoDate1 = [geoCalender dateFromComponents: comps1];
    
    NSLog(@"date: %@", date2);
    NSLog(@"date: %@", date3);
    NSLog(@"date: %@", geoDate1);


In the above code snippet return the output dates as follow


2019-12-06 20:27:18.705789+1100 SampleiOS[78970:498109] date: Thu Oct 3 23:00:00 2019

2019-12-06 20:27:18.706128+1100 SampleiOS[78970:498109] date: Thu Oct 3 22:00:00 2019

2019-12-06 20:27:18.706467+1100 SampleiOS[78970:498109] date: Thu Oct 3 22:00:00 2019

2019-12-06 20:27:18.706831+1100 SampleiOS[78970:498109] date: Mon Oct 7 23:00:00 2019

2019-12-06 20:27:18.707043+1100 SampleiOS[78970:498109] date: Mon Oct 7 23:00:00 2019

2019-12-06 20:27:18.707201+1100 SampleiOS[78970:498109] date: Mon Oct 7 23:00:00 2019


Can you please explain which creation of NSDate as correct?

Replies

In the above code snippet return the dates as follow

But nothing followed. Can you elaborate and what you saw and what you expected to see?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I was updated the output content

2019-12-06 20:27:18.705789+1100 SampleiOS[78970:498109] date: Thu Oct 3 23:00:00 2019

2019-12-06 20:27:18.706128+1100 SampleiOS[78970:498109] date: Thu Oct 3 22:00:00 2019

2019-12-06 20:27:18.706467+1100 SampleiOS[78970:498109] date: Thu Oct 3 22:00:00 2019

2019-12-06 20:27:18.706831+1100 SampleiOS[78970:498109] date: Mon Oct 7 23:00:00 2019

2019-12-06 20:27:18.707043+1100 SampleiOS[78970:498109] date: Mon Oct 7 23:00:00 2019

2019-12-06 20:27:18.707201+1100 SampleiOS[78970:498109] date: Mon Oct 7 23:00:00 2019

What would be the correct date, for :

2019-12-06 20:27:18.705789+1100 SampleiOS[78970:498109] date: Thu Oct 3 23:00:00 2019


How did you set your locale time zone ?

Times are all universal times; right now it is 20:38 on the US East Coast but 17:38 on the US Pacific Coast - both Standard time not daylight savings time. You did not specify a time zone on the calendars that displayed “23:00”. They defaulted to GMT Standard time. You set a time zone for the two calendars that are displaying “22:00”. The ‘time’ is the same, it’s the time zones that are different.