Vaisakh
Vaisakh

Reputation: 1088

How to set date formatter for the date structure "2014-04-01T13:30:00.000+0000"

I'm new in objective C. I want to set a date formatter for converting date "2014-04-01T13:30:00.000+0000" to another format - "2014/04/01 13:30:00 AM".

I have already tried many formats, but its not working. Please help to solve this issue.

Upvotes: 3

Views: 233

Answers (3)

user907729
user907729

Reputation:

use below format, will work... :)

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.sssZ"];
NSDate *date = [df dateFromString:str];
NSLog(@"%@",date);

for more ref see this.

Upvotes: 1

Kundan
Kundan

Reputation: 3082

This code works well for me and think works for you too..

NSDate *today = [NSDate date];
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"yyyy/MM/dd hh:mm:ss a"];
 NSString *dateString = [dateFormat stringFromDate:today];
 NSLog(@"date: %@", dateString);
 [dateFormat release];

If it doesn't work for you..Feel free to ask..Just comment..

Upvotes: 0

Anupdas
Anupdas

Reputation: 10201

Try this

NSString *dateFormat1 = @"yyyy-MM-dd'T'HH:mm:ss.sssZ";
NSString *dateFormat2 = @"yyyy/MM/dd HH:mm:ss a";

NSString *dateString = @"2014-04-01T13:30:00.000+0000";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:dateFormat1];

NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:dateFormat2];
dateString = [dateFormatter stringFromDate:date];

NSLog(@"Date : %@",dateString);

Upvotes: 1

Related Questions