开发者

Converting GMT date format to locale based format

开发者 https://www.devze.com 2023-02-22 03:53 出处:网络
How 开发者_JS百科can we convert NSDate object in GMT format to the date format with current locale/device settings?

How 开发者_JS百科can we convert NSDate object in GMT format to the date format with current locale/device settings?

For example if a device is in US and in PDT time zone then I want to convert GMT time to its equivalent PDT time.


Strictly, you cannot convert an NSDate to another time zone because NSDate has no conception of time zones whatsoever. An NSDate object simply represents a single point in (absolute) time, regardless of time zone.

Whenever you want to display a date to the user, you should create an NSDateFormatter. By default, it automatically displays time in the user's time zone. You can override this with -[NSDateFormatter setTimeZone:].


Just use the NSDate method descriptionWithCalendarFormat:timeZone:locale: passing in the appropriate timezone. If you just use the description method you get the date/time in the current timezone (so if the device is set to PDT the time will be in PDT).

0

精彩评论

暂无评论...
验证码 换一张
取 消