newbie
newbie

Reputation: 1059

issue with date formatting using NSDateFormatter

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 ?

Upvotes: 3

Views: 14786

Answers (3)

prince
prince

Reputation: 924

Replace your code with this...

NSString * date_string = @"2013-06-04 02:19:21 +0000";
NSDateFormatter *complexdateFormater = [[NSDateFormatter alloc] init];
[complexdateFormater setDateFormat:@"EEE, MMM dd"];
NSDate* complexdate = [complexdateFormater dateFromString:date_string];

If you want to check the date do this instead of NSLog as NSLog returns date in GMT format only...

NSLog(@"Converted date: %@", [complexdateFormater stringFromDate:complexDate]);

Upvotes: 1

Andrew
Andrew

Reputation: 15377

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:

[complexdateFormater setDateFormat:@"yyyy-M-DD HH:mm:ss Z"];

Should be this:

[complexdateFormater setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

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.

Upvotes: 0

Sam B
Sam B

Reputation: 27618

Here you go my friend

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEE, MMM dd"];
    NSString *theDate = [dateFormat stringFromDate:date1];

Date formatter reference for everyone else that might visit this post. Do rate it up if it helped!

/*
     x           number
     xx          two digit number
     xxx         abbreviated name
     xxxx        full name

     a           AM/PM
     A           millisecond of day
     c           day of week (c,cc,ccc,cccc)
     d           day of month
     e           day of week (e,EEE,EEEE)
     F           week of month
     g           julian day (since 1/1/4713 BC)
     G           era designator (G=GGG,GGGG)
     h           hour (1-12, zero padded)
     H           hour (0-23, zero padded)
     L           month of year (L,LL,LLL,LLLL)
     m           minute of hour (0-59, zero padded)
     M           month of year (M,MM,MMM,MMMM)
     Q           quarter of year (Q,QQ,QQQ,QQQQ)
     s           seconds of minute (0-59, zero padded)
     S           fraction of second
     u           zero padded year
     v           general timezone (v=vvv,vvvv)
     w           week of year (0-53, zero padded)
     y           year (y,yy,yyyy)
     z           specific timezone (z=zzz,zzzz)
     Z           timezone offset +0000

     sql         y-M-d H:m:s
     rss         [E, ]d MMM y[y] H:m:s Z|z[zzz]
     */

Upvotes: 13

Related Questions