32

How can i convert the following date "Sun Jul 17 07:48:34 +0000 2011" to the following format "2011-07-17 07:48:34"?

I used NSDateFormatter as shown below but it didnt work. It gives null as a result.

NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init];
[objDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[objDateFormatter dateFromString:sDate];
Ares
  • 5,905
  • 3
  • 35
  • 51
farajnew
  • 926
  • 2
  • 9
  • 15
  • Can you please add the code you used with NSDateFormatter? – Sandro Meier Jul 17 '11 at 13:40
  • [NSDate format in iOS 4.1](http://stackoverflow.com/questions/3716384/nsdate-format-in-ios-4-1) –  Jul 17 '11 at 13:44
  • 1
    possible duplicate of [iPhone: Convert date string to a relative time stamp](http://stackoverflow.com/questions/902950/iphone-convert-date-string-to-a-relative-time-stamp) – Ayaz Aug 22 '13 at 11:57

2 Answers2

71

You've got your Date Format wrong for the style of Date you are passing in. Here is a document explaining the different modifiers: Date Format Patterns

To parse the Date "Sun Jul 17 07:48:34 +0000 2011", you'd need a Format like so:

[dateFormat setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];

To get it into the following format: "2011-07-17 07:48:34", here is the full code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSDate *date  = [dateFormatter dateFromString:sDate];

// Convert to new Date Format
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:date]; 
mmcdole
  • 91,488
  • 60
  • 186
  • 222
Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
  • A problem ... it works fine on simulator but it doesnt work on real device ? – farajnew Jul 17 '11 at 14:26
  • What problem do you have on the Device? Does it give an error? Crash? You need to describe your problem... – Suhail Patel Jul 17 '11 at 14:28
  • it gives null at converting at any step ... but on simulator it gives the exact result for example date equals null , new date null but on simulator it works perfectly i tried it on two devices ... the same problem – farajnew Jul 17 '11 at 14:39
  • If it works on the Simulator but not the device then it may be another issue with your other code. I've used this exact code with both the Simulator and Device multiple times in my projects and I've only seen issues where the dateFormat is incorrect. Maybe post your Project or some more code. – Suhail Patel Jul 17 '11 at 14:47
  • NSString *sStatusDate= [objStatusDic valueForKey:@"created_at"]; NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init]; [objDateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZZZ yyyy"]; NSDate *objDate = [objDateFormatter dateFromString:sStatusDate]; [objDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; objStatus.StatusDate = [objDateFormatter stringFromDate:objDate]; – farajnew Jul 17 '11 at 14:49
  • Well if both dates are null, it could possibly be that the value of sStatusDate is null on the device? Do an NSLog on sStatusDate and post the output – Suhail Patel Jul 17 '11 at 19:09
2

If sDate is your string that's in the format "Sun Jul 17 07:48:34 +0000 2011", you have to convert that into a NSDate. Then, you can use a second NSDateFormatter to convert this date into your desired format "yyyy-MM-dd HH:mm:ss".

If you need to figure out the string needed to convert, this site has a great reference: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

e.g. "Sun Jul 17 07:48:34 +0000 2011" can be parsed with

[dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss ZZZ yyyy"];

As a bit of advice, non-standard dates like "Jul" (should be July) can make problems here. It's faster to just convert the needed parts in plain C, with strftime(). See the reference: http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/rzan5mst263.htm

You can then convert the unix timestamp back in a NSDate with

[NSDate dateWithTimeIntervalSince1970:]

Using C for date/time parsing is usually up to 10x faster. NSDateFormatter is a slow dog.

steipete
  • 7,581
  • 5
  • 47
  • 81