0

I have this date string - "2011-07-04T00:00:00", and I would like to display the date as "July 7, 2011"

This is the code I'm using to convert the date string to the formatted date string -

NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SS"];
NSDate *date = [f dateFromString:@"2011-07-04T00:00:00"];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString* formattedDate = [formatter stringFromDate: date];

Is this the best way?

jscs
  • 63,694
  • 13
  • 151
  • 195
bickster
  • 1,292
  • 17
  • 18
  • Is it working at it is now? – Neeku Aug 15 '14 at 13:18
  • 1
    Yep, I don't think any better way. You have to use two times date formatter styles in your case. – Paresh Masani Aug 15 '14 at 14:02
  • No, it's not the best way, since it essentially tosses the seconds value in the trash can. Read [the standard](http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns) to see what you've done wrong. – Hot Licks Aug 15 '14 at 15:26
  • And you need to be aware of [this issue](http://stackoverflow.com/q/6613110/581994). – Hot Licks Aug 15 '14 at 15:28

1 Answers1

2

its is always better to use system format for date to prevent localization issues .

Different countries and regions have different conventions for formatting numerical and time-based information. Locale settings provide information about the formats used by the current user and must be considered when writing code that handles user-facing data types. The user sets the locale by choosing a region in Settings on iOS devices and System Preferences on a Mac. The user can also change locale settings while the app is running. Therefore, if you manipulate data objects in your code and then present them to the user, use the locale APIs to format the data correctly.

https://developer.apple.com/library/prerelease/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingLocaleData/InternationalizingLocaleData.html