3

When I log an NSDate to the console like this:

NSDate *date = [NSDate date];
NSLog(@"date: %@", date);

it logs the output:

date: Fri Nov 17 09:35:27 2017

I was expecting the output to be more like this:

date: 2017-11-17 09:35:27 +0000

For a long time [NSDate description] returned the date as a string in the GMT time zone no matter what the time zone of the machine was set to. Now the output changes when you change the time zone of the machine (although it doesn't print the time zone that it's using).

Have I gone crazy? Is the output different that it was before? Setting the deployment target to an earlier OS makes no difference to the output. If it's different, when did it change? Is it documented?

Edit: CFDate still gives the old description:

CFAbsoluteTime      absTime;
CFDateRef           aCFDate;
absTime = CFAbsoluteTimeGetCurrent();
aCFDate = CFDateCreate(kCFAllocatorDefault, absTime);
CFStringRef dateDescription = CFCopyDescription(aCFDate);
NSString *description = (__bridge NSString *)dateDescription;
NSLog(@"%@", description);

2017-11-17 10:50:19 +0000

Another Edit (Xcode Version 9.2):

Objective-C has different output depending on how the date is logged:

NSDate *date = [NSDate date];
NSLog(@"date: %@", date);
NSLog(@"date: %@", date.description);
NSLog(@"date: %@", date.debugDescription);

date: Tue Jan 23 10:19:29 2018
date: 2018-01-23 10:19:29 +0000
date: 2018-01-23 10:19:29 +0000

I had thought that logging an object directly was the same as logging its description.

Swift has the old behaviour:

let now = Date()
print("\(now)")
print("\(now.description)")
print("\(now.debugDescription)")

2018-01-23 10:19:00 +0000
2018-01-23 10:19:00 +0000
2018-01-23 10:19:00 +0000

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • _"Setting the **deployment target** to an earlier OS"_ The SDK that you're linking against is called the "Base SDK", which is what you'd want to change to test this. – jscs Nov 17 '17 at 13:44
  • @JoshCaswell Thanks, I had a feeling deployment target was the wrong one. Too bad Base SDK is so much more difficult to change. – nevan king Nov 17 '17 at 14:29

1 Answers1

-1

Set Date formatter like below

NSTimeZone *timeZone = [NSTimeZone defaultTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];