Carl Hung
Carl Hung

Reputation: 555

UIDatePicker outputs different formats of string of date between simulator and iPhone

I ran the UIDatePicker on my iPhone and on the simulator, they both show output in different formats?

How can i solve the problem? Is there a format that can work around? I know it is because of NSLocale. as I want my app not only to work on one country. I need save the date to the server for searching and comparing. Therefore, I need a format to work around.

Thanks.

Upvotes: 1

Views: 686

Answers (2)

Ashley Mills
Ashley Mills

Reputation: 53161

The date format displayed on a UIDatePicker is, as you say, dependant on the locale of the device. The simulator is set to en_US by default, I assume your device is different.

Do NOT force user to us a standard format - the date should be displayed in the picker in their device's locale format - using a standard format will be confusing for most of your users. For example, a user in USA would expect to see their dates as MM/DD/YY, in UK DD/MM/YY, in South Africa as YY/MM/DD. If you force a standard format it will be wrong for most of your users. See http://en.wikipedia.org/wiki/Date_format_by_country

Regardless of what format is displayed in a UIDatePicker, the date selected by the user and is just a Date object. This does NOT have a format, it is just a point in time.

If you're comparing / searching dates, never use a formatted date string. Use Date, Calendar, etc methods.

If you print the value of a Date object, it will format it using the current locale (but that is just how it's displayed, the date itself has no format)

See Apple docs for more Date info

If you want to send your selected date to the server in a fixed format, you should use DateFormatter and set the format/locale to whatever your require. Apple's recommendation is the use en_US_POSIX as the locale. Only use this internally, when converting your date to a format for sending to the server - this shouldn't be seen by the user.

From Apple docs

let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(forSecondsFromGMT: 0)

/* 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time) */
let string = "1996-12-19T16:39:57-08:00"
let date = RFC3339DateFormatter.dateFromString(string)

In iOS 10+, this has been simplified - you can just use the ISO8601Date​Formatter class

Upvotes: 2

Lepidopteron
Lepidopteron

Reputation: 6165

I would recommend to keep the date format a user had selected and NOT modify it, because this is what users do expect.

Step 1) In case you need to further process the selected date by another function or want to send it to a server in a certain format, create a NSDate object with the locale specific dateformatter: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/InternationalizingLocaleData/InternationalizingLocaleData.html

Step 2) And use another NSDateFormatter that firs to your needs to now transform the NSDate to a NSSTring you can send to somehwere else in a standardizes format. https://developer.apple.com/reference/foundation/nsdateformatter

Another option would be to only display the locale formatted date, but keep a reference to the original Date Object, this way you could skip step 1

Upvotes: 0

Related Questions