Ankit Srivastava
Ankit Srivastava

Reputation: 12405

Confusion regarding [NSDate date]

[NSDate date] returns GMT on simulator... what time does it return on the device... ? And also how can I get the simulator time programmatically?

Upvotes: 1

Views: 821

Answers (2)

kennytm
kennytm

Reputation: 523314

It returns the current date and time. NSDate is timezone-independent. The -description of NSDate may be in GMT, but it's not guarenteed.

The representation is not guaranteed to remain constant across different releases of the operating system. To format a date, you should use a date formatter object instead.

If you want to display or parse a date in a different timezone, use NSDateFormatter (and modify its timezone/locale property) or use -descriptionWithLocale:.

NSLocale* currentLocale = [NSLocale currentLocale]
NSLog(@"current locale = %@", [currentLocale localeIdentifier]);
NSLog(@"date = %@", [[NSDate date] descriptionWithLocale:currentLocale];

Upvotes: 6

Hot Licks
Hot Licks

Reputation: 47729

NSDate is timezone-independent. When you get the description with description it displays a timezone but that's entirely "informational", and, to my knowledge there's no way to set or access that timezone info (other than description). The value stored in the NSDate object is (supposed to be) GMT/UTC (though of course it can't tell if you're feeding it the wrong time).

To get the simulator's understanding of the current time and date use [NSDate date]. Format it (if need be) with NSDateFormatter.

Upvotes: 1

Related Questions