Reputation: 869
I am getting data in following format
20130224233000 where,
2013 - year
02 month
24 day
23 hh
30 mm
00 ss
and I want in following format
Wednesday, 24-Feb-13 11:30 PM
I tried as below code
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
return dateString;
but it give a format which I don"t required
Upvotes: 0
Views: 4105
Reputation: 8805
You need two NSDateFormatters, one to convert to NSDate and the other to NSString. Use the format table as reference.
NSString* date = @"20130224233000";
NSDateFormatter *parser = [[NSDateFormatter alloc] init];
[parser setDateFormat:@"yyyyMMddHHmmss"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"eeee, dd-MMM-yy hh:mm a"];
NSString *dateString = [formatter stringFromDate:[parser dateFromString:date]];
return dateString;
Upvotes: 3
Reputation: 9836
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[dateFormat setDateFormat:@"EEEE, dd-MMM-yy HH:mm a"];
NSString *dateString = [dateFormat stringFromDate:today];
return dateString;
Hope this helps.
Upvotes: 0
Reputation: 1847
Try this:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE d-MMM-yy HH:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
return dateString;
Upvotes: 0