Anas Abu Farraj
Anas Abu Farraj

Reputation: 1598

Can Xcode log console be modified to output the time in local machine timezone instead of GMT?

Can Xcode log console be modified to get the time output in local timezone but not in 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

Upvotes: 1

Views: 488

Answers (1)

Paulw11
Paulw11

Reputation: 114975

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

Upvotes: 2

Related Questions