0

Possible Duplicate:
convert string to nsdate

I have this string Fri, 07 Dec 2012 08:40:33 +0100

How can I convert it into NSDate

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM YYYY hh:mm:ss +HHmm"];
NSDate *myDate=[dateFormatter dateFromString:@"Fri, 07 Dec 2012 08:40:33 +0100"];
NSLog(@"-->%@",myDate);

The nslog gives

-->2012-12-06 19:30:33 +0000

This code is not working properly kindly help. Thanks!!!

Community
  • 1
  • 1

3 Answers3

2

You code is almost correct, but there are some mistakes.

First the year is not YYYY but just yyyy and hh is not the 24 hour is incorrect since the hours are present as 24 hours thus you shouhd use HH. Next is the timezone offset, wich is not +HHmm but just Z. And now the most importen part, your date has the day and a month as a written language but the app might run on an non english language os the parsing will not work, you will have to add a locale.

The full code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"EN"]];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *myDate=[dateFormatter dateFromString:@"Fri, 07 Dec 2012 08:40:33 +0100"];
NSLog(@"-->%@",myDate);
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • it prints 07:40, i need 08:40 in time –  Dec 10 '12 at 09:28
  • No the 07:40 is correct, the time is present as GMT, wich is offset to you time by 1 hour, thus if you use NSDateFormatter with the system timezone the correct time is show relative to the timezone offset. – rckoenes Dec 10 '12 at 09:44
  • Yes, now i understood. I was very silly of this. –  Dec 10 '12 at 09:45
  • I cant vote up, how can I, i dont hve 15 points. –  Dec 10 '12 at 09:46
  • I earned vote up, so for you sir. –  Dec 10 '12 at 10:02
1

I checked your comments on all the answers, and I woul like to say, you are getting confused with the time zone.

07:40 is the time of GMT, and while you are having 08:40 +1000 (one hour 00 min) so these are same. Similarly while regenerating and printing it will print in GMT.

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"EN"]];
NSDate *myDate=[dateFormatter dateFromString:@"Fri, 07 Dec 2012 08:40:33 +0100"];
NSLog(@"--> %@",myDate);


NSString *rechange=[dateFormatter stringFromDate:myDate];

NSLog(@"==> %@",rechange);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • The `hh` hour format is incorrect, since there is not AM/PM switch in the date you should have used the 24 hour `HH` format string. – rckoenes Dec 10 '12 at 09:46
  • 1
    And you code will fail on iOS devices where the default language is not set to english, like may answer stated you need to supply the local. – rckoenes Dec 10 '12 at 09:50
  • so how to do that? `[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"EN"]];` has to added?? – Anoop Vaidya Dec 10 '12 at 09:54
  • is der diffnce between iphone and mac? me wanted for mac only. and this work fine –  Dec 10 '12 at 09:58
  • 2
    Well if the system even OSX is set to Dutch for example, datformatter will try to parse the date as a Dutch date. The Dec will be fine but May will be Mei, which it will not be able to parse, there you should always set the the locale if the date contains localized elements. – rckoenes Dec 10 '12 at 10:04
  • This is what I learnt today...Thanks a lot rckoenes. – Anoop Vaidya Dec 10 '12 at 10:06
0

I think you need you change your final +HHmm to ZZZZ.

It's the time zone, not the time again.

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114