get NSDate from NSString
I have cur开发者_运维知识库rent date and time in PST format as a string such as
currentDateStr 06/02/11 05:40:33 PDT
Could anyone tell me how to convert this string into NSDate.
Thanx in advance.
From this SO question NSDate expressed in different time zones, i.e. local time zone (GMT-400) to PST
NSDate always returns relative to 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.
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
I guess your date formatter settings are wrong. Try with the following:
[formatter setDateFormat: @"MM/dd/yy HH:mm:ss a"];
Best of luck
If you are using a ISO8601 complint string for the date I recommend using the parser as made by Peter Hosey http://boredzo.org/iso8601dateformatter/
NSDate *isoDateFromString = [yourISO8601dateformatterObject dateFromString:iso8601DateString];
This will give you a NSDate without you fussing with timezone and stuff.
精彩评论