How to convert a UTC date to NSDate?

Ditto on the need to alloc/init your NSDateFormatter object, but you have another problem too: your Date format string does not match the actual date you're giving it.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I have a string that is UTC and would like to convert it to an NSDate. Static NSDateFormatter* _twitter_dateFormatter; _twitter_dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault; _twitter_dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"; _twitter_dateFormatter setLocale:_en_us_locale; NSDate *d = _twitter_dateFormatter dateFromString:sDate; When I go through the debugger d is nil even though sDate is "2010-03-24T02:35:57Z" Not exactly sure what I'm doing wrong. Iphone objective-c nsdate nsdateformatter link|improve this question edited Mar 24 '10 at 5:06 asked Mar 24 '10 at 2:37Sheehan Alam5,002360179 91% accept rate.

Also just a little nit-pick: d is nil in the debugger not *d (you can't dereference a null pointer). – Alan Rogers Mar 24 '10 at 2:46.

Ditto on the need to alloc/init your NSDateFormatter object, but you have another problem too: your Date format string does not match the actual date you're giving it. "EEE MMM dd HH:mm:ss ZZZ yyyy" - vs - "2010-03-24T02:35:57Z" That format string would match something like: "Wed Mar 24 00:07:33 -0400 2010" See the unicode standard for the meaning of the date format string.

Thanks for pointing out the mismatch. I'm not sure how I can format the string to be like "2010-03-24T02:35:57Z" – Sheehan Alam Mar 24 '10 at 5:06 Similar question stackoverflow.com/questions/1263455/… In particular, note the suggestion that you may want to replace the final "Z" in the input string with an explicit numeric timezone (-0000) – David Gelhar Mar 24 '10 at 5:14 Thanks! In my case the date format should be yyyy-MM-dd'T'HH:mm:ssZ Replacing Z with (-0000) did the trick.

– Sheehan Alam Mar 24 '10 at 5:31.

You aren't allocating or initialising your NSDateFormatter object. Try something like this: NSDateFormatter* _twitter_dateFormatter = NSDateFormatter alloc init; _twitter_dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault; _twitter_dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"; _twitter_dateFormatter setLocale:_en_us_locale; NSDate *d = _twitter_dateFormatter dateFromString:sDate; _twitter_dateFormatter release; It's also unclear why you are declaring _twitter_dateFormatter as a static. If you are trying to avoid re-allocating it, make it an ivar of your class.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions