从NSString获取NSDate [英] get NSDate from NSString

查看:162
本文介绍了从NSString获取NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以PST格式当前的日期和时间为字符串,例如

I have current date and time in PST format as a string such as

currentDateStr 06/02/11 05:40:33 PDT

任何人都可以告诉我如何将此字符串转换为NSDate。

Could anyone tell me how to convert this string into NSDate.

Thanx提前。

推荐答案

从这个SO问题NSDate表示在不同的时区,即本地时区( GMT-400)到PST


NSDate always returns relative to GMT


这没有意义。 NSDate刚刚封装了一个绝对的时刻(让我们忘了一秒钟的相对性),它没有时区的概念。要说NSDate时间是相对于GMT是错误的。

This doesn't make sense. NSDate just encapsulates an absolute moment in time (let's forget about relativity for a second) and it has no concept of time zones whatsoever. To say that NSDate times are relative to GMT is wrong.

要在特定时区输出日期,您应该创建一个NSDateFormatter的实例并调用setTimeZone:on设置时区。根据Unicode文档,格式字符串@zzz应将时区值输出为PST。

To output a date in a specific time zone, you should create an instance of NSDateFormatter and call setTimeZone: on it to set the time zone. According to the Unicode docs, the format string @"zzz" should output the time zone value as "PST".

NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"PST"]; // for PST

这篇关于从NSString获取NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆