Reputation: 2310
I am attempting convert this string "2012-02-05T00:00:00+00:00" into a more attractive, nicely formatted string like "Tuesday March 5, 2012.
I have an example that I'm working from:
NSString *dateStr = @"20081122";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(@"date: %@", dateStr);
[dateFormat release];
How would I accommodate for the date format "2012-02-05T00:00:00+00:00" using similar code above?
Thank you.
Upvotes: 0
Views: 3942
Reputation: 8501
When i see your time "2012-02-05T00:00:00+00:00", I think you are printing a "NSDate" into your console. In that time "2012-02-05" is a date and "T00:00:00+00:00" is a time. So when you need to use date like this,
NSString *dateStr = @"2012-02-05";
you have to format like this
[dateFormat setDateFormat:@"yyyy-MM-dd"];
If you want to convert the date component into as you liked try this
NSString *dateStr = @"2012-02-05";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(@"%@",dateStr);
You can even do like this to convert the current date into as you like.
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:[NSDate date]]; //for current date
NSLog(@"%@",dateStr);
Upvotes: 1
Reputation: 11145
You can do by using -
[dateFormat setDateFormat:@"yyyy-MM-ddThh:mm:ss z"];
Upvotes: 3