I have a date string which I want to convert to another format.
The original date string example is : "2013-06-04 02:19:21 +0000"
I want to convert this to "Wed, Jun 4"
NSString * date_string = @"2013-06-04 02:19:21 +0000";
NSDateFormatter *complexdateFormater = [[NSDateFormatter alloc] init];
[complexdateFormater setDateFormat:@"yyyy-M-DD HH:mm:ss Z"];
NSDate* complexdate = [complexdateFormater dateFromString:date_string];
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"EE, MMM d" options:0
locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:complexdate];
but the today string for the above case prints out : the month as Jan irrespective of the month value in the original string.
where am i going wrong ?
Well to start, you have two quotes after +0000. Im not sure if this is a typo in this post or not, but it could effect something.
This Line:
Should be this:
in your date string the month is 06 not just 6 so you needed two month digits :) Also, you needed DD to be dd, as rmaddy pointed out below.
Here you go my friend
Date formatter reference for everyone else that might visit this post. Do rate it up if it helped!
Replace your code with this...
If you want to check the date do this instead of NSLog as NSLog returns date in GMT format only...