NSDate / NSDateFormatter returning GMT on iPhone iOS 4.3

后端 未结 2 1788
温柔的废话
温柔的废话 2021-01-25 14:32

Why would this code be giving me GMT? (I am in US Mountain Time)

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
[dateFormatter s         


        
相关标签:
2条回答
  • 2021-01-25 15:12

    Yes, turns out this was a bug in Apple's Numbers on the Mac. Numbers was not interpreting the date string, or rather it was adding the time offset. The NSDate string, after formatting, was correct all along.

    0 讨论(0)
  • 2021-01-25 15:25

    An NSDate represents a concrete point in time, regardless of the timezone. Put another way, an NSDate does not have a timezone. Timezones are only relevant when you want to display the date to the user. So 9:30pm in Mountain Time is 3:30am (+1 day) in GMT (assuming a 6 hour time difference).

    NSDate, since it does not have a timezone, must pick one when producing a human-readable version to return as its -description. To make things simple, it always returns a date formatted in the GMT time zone. If you would like the date formatted to be in a different timezone, you can set the -timezone property of an NSDateFormatter, and then convert the date into a string using the -stringFromDate: method.

    0 讨论(0)
提交回复
热议问题