anasabufarraj anasabufarraj - 5 months ago 16
iOS Question

Can i change the Xcode log console to output the time in local machine timezone (not GMT)?

Can I change something to the Xcode log console to get the time output in local timezone (not GMT)?

I use the following code:

NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setYear:1979];
[comp setMonth:5];
[comp setDay:10];
[comp setHour:10];
[comp setMinute:30];
[comp setSecond:0];
[comp setTimeZone:[NSTimeZone localTimeZone]];

NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *dateOfBirth = [gregorian dateFromComponents:comp];
NSLog(@"\nDate of birth in GMT %@\n\n", dateOfBirth);


That produces the following output in the log console:

Date of birth in GMT 1979-05-10 08:30:00 +0000

Answer

When you evaluate an NSDate in this way it actually calls the description method, which returns the date in UTC. If you wanted to display a date to your user you should use an NSDateFormatter, but for simple logging you can use descriptionWithLocale: -

NSLog(@"\nDate of birth in local time zone %@\n\n", [dateOfBirth descriptionWithLocale:[NSLocale currentLocale]]);
Comments