0

I have an date string which looks like this:

2011-10-01 08:45:34 +0000

How can I format it with NSDate? Thanks :D

Maksim
  • 2,054
  • 3
  • 17
  • 33
pmerino
  • 5,900
  • 11
  • 57
  • 76

1 Answers1

3

you can try this method in NSDateFormatter

- (NSDate *)dateFromString:(NSString *)string

an example

dateFormatter = [[NSDateFormatter alloc] init];

NSLocale *en_US = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:en_US];
[en_US release];

[dateFormatter setDateFormat:@"MM/dd/yyyy h:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-60*60*7]];
NSDate *date = [dateFormatter dateFromString:dateString];

[dateFormatter release];
salahy
  • 1,177
  • 9
  • 9
  • Important: If you want the date to always display the same, [explicitly assign the local of the formatter](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformater-locale-feature). – Hot Licks Oct 01 '11 at 12:16