NSDate / NSDateFormatter returning GMT on iPhone iOS 4.3

后端 未结 2 1790
温柔的废话
温柔的废话 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: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.

提交回复
热议问题