dragon
dragon

Reputation:

NSDateFormatter Iphone

I want to convert this ,

NSString *result1=@"Mon, 28 Sep 2009 06:35:42 PDT";

to nsdate using NSDateFormatter in iphone....

Can anyone help me?

Thanks in advance.....

Upvotes: 0

Views: 1796

Answers (2)

nall
nall

Reputation: 16129

I believe you want this:

NSString* dateString = @"Mon, 28 Sep 2009 06:35:42 PDT";

NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];

// Use this format to parse the string
[newFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];

// Now change the format to that desired for printing
[newFormatter setDateFormat:@"MMM dd,yyyy HH:mm:ss aaa"];
NSLog(@"%@", [newFormatter stringFromDate:aDate]);

// Result:
// 2009-09-29 23:50:09.440 test[15396:903] Sep 28,2009 06:35:42 AM

You can find these codes here (as referenced in the NSDateFormatter documentation): http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Upvotes: 4

Nava Carmon
Nava Carmon

Reputation: 4533

That's what I do in my program:

NSString *dateString = @"Mon, 28 Sep 2009 06:35:42 PDT";

NSDateFormatter *newFormatter = [[NSDateFormatter   alloc] init];
[newFormatter setDateStyle:NSDateFormatterMediumStyle];
[newFormatter setTimeStyle:NSDateFormatterMediumStyle];

I'm using medium style, but there are more styles, you probably should use kCFDateFormatterLongStyle or kCFDateFormatterFullStyle, and then:

NSDate *aDate = [newFormatter dateFromString: dateString];
[newFormatter   release];

Hope this helps

Upvotes: -1

Related Questions