XCODE IOS 5 - Date Format

Hi Experts,

How would I format the following date in xcode:

2012-08-21T19:29:52Z

to

dd, MMM yyyy?

Thanks
rmartesAsked:
Who is Participating?
 
pgnatyukCommented:
So the following code works:

        
       NSString *input = @"2012-08-21T19:29:52Z";
        NSString *modified = [input stringByReplacingOccurrencesOfString:@"T" withString:@""];
        modified = [modified stringByReplacingOccurrencesOfString:@"Z" withString:@""];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
        NSDate *output = [dateFormatter dateFromString:modified];
        NSLog(@"the date is %@", output);

Open in new window


The output:
2012-08-24 18:59:51.854 DateTimeFormatTest[71926:403] the date is 2012-08-21 16:29:52 +0000
0
 
rmartesAuthor Commented:
Anyone care to give a shot? I tried the normal ios dateformatter similar to:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
 
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
 
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);

But no luck
0
 
pgnatyukCommented:
I just tried and don't solve the question.

In this way:
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH-mm-ss'Z'"];
        NSString *test = [dateFormatter stringFromDate:[NSDate date]];
        NSLog(@"the date is %@", test);

Open in new window


I get the string:
the date is 2012-08-24T12-52-14Z
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Hamidreza VakilianSenior iOS DeveloperCommented:
You mean you want to parse a string like "2012-08-21T19:29:52Z" and then convert it to dd, MMM yyyy format? If yes you can easily separate the input string (tokenize it) and pass it to NSDate to get the date components.
0
 
rmartesAuthor Commented:
Thanks guys...

Programmer-x, Yes that's exactly what I want to do....but how would I do that?
0
 
rmartesAuthor Commented:
Great!! Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.