0

NSDateFormatter doesn't seem to like October Eighteenth. Tried every other date with success. (???)

It is almost certainly a bug in swift 2.0 (and if it is what should I do? I need to submit my app really soon). Am I missing something?

Checked it in playground on Xcode 7.0.1:

let str = "2015-10-18"
let str2 = "2015-10-14"

let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"

var date = formatter.dateFromString(str)
var date2 = formatter.dateFromString(str2)

Output:

"2015-10-18"
"2015-10-14"

<NSDateFormatter: 0x7f8aa050b760>
<NSDateFormatter: 0x7f8aa050b760>
<NSDateFormatter: 0x7f8aa050b760>

nil
"Oct 14, 2015, 12:00 AM"
Bruno Galinari
  • 155
  • 2
  • 12
  • It's certainly *not* a bug in Swift 2 since I just used your code [without errors](https://www.evernote.com/l/AOxUu3wld7hOmJRbAZ-5Gril9A9Jp_FMbyc). :) – Eric Aya Oct 06 '15 at 13:20
  • @EricD. that's odd. I have same error. Which XCode version are you using? – Caio Oct 06 '15 at 13:27
  • @Caio Xcode 7 GM. Also works in Xcode 7.1 beta. – Eric Aya Oct 06 '15 at 13:28
  • 2
    What timezone are you using? `America/Campo_Grande`, `America/Cuiaba`, and `America/Sao_Paulo` returns `nil` – rintaro Oct 06 '15 at 13:33
  • I don't know why, but it's related to ours Daylight Saving Time date (We both have same default Time Zone). If you use the 2016 initial DST date ("2016-10-16") you will have the same issue. – Caio Oct 06 '15 at 13:40
  • You need to specify the calendar. Probably a bug – Leo Dabus Oct 06 '15 at 13:40
  • 1
    Thank you Leo Dabus!! You got it. As I don't specify the hour, it assumes it's midnight, but as it's daylight first day, there's no midnight, jumps straight to 1AM. – Bruno Galinari Oct 06 '15 at 13:44

1 Answers1

1

The behavior depends on which timeZone the formatter using.

For example: America/Sao_Paulo

In Sao Paulo, http://www.timeanddate.com/time/change/brazil/sao-paulo?year=2015

Sunday, 18 October 2015, 00:00:00 clocks are turned forward 1 hour to Sunday, 18 October 2015, 01:00:00 local daylight time instead

That means, there is no 2015-10-18 00:00:00 in Sao Paulo.

And as for NSDateFormatter, when it receives no time information from string, it assumes the time is 00:00:00 in its timezone. That's why it returns nil.

let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(name: "America/Sao_Paulo")
formatter.dateFormat = "yyyy-MM-dd HH:mm"

formatter.dateFromString("2015-10-18 00:00") // -> nil
formatter.dateFromString("2015-10-18 00:30") // -> nil
formatter.dateFromString("2015-10-18 01:00") // -> non nil

As @LeoDabus stated, when you specify .calendar on the formatter:

it assumes the time is 00:00:00 in its timezone.

it will assume the start of the day for that date.

let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(name: "America/Sao_Paulo")
formatter.dateFormat = "yyyy-MM-dd"
formatter.calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)

formatter.dateFromString("2015-10-17") // -> Oct 17, 2015, 12:00 AM
formatter.dateFromString("2015-10-18") // -> Oct 18, 2015, 1:00 AM
formatter.dateFromString("2015-10-19") // -> Oct 19, 2015, 12:00 AM
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • 1
    This behavior actually depends if you specify a calendar or not. If you do it will assume the start of the day for that date for a specific locale. In that case 1am so there is no need to specify the time. – Leo Dabus Oct 06 '15 at 14:12
  • HI Leo, I had same problem using Objective-C, could you help me with Objective-C similar code ? – Anibal Lamego Oct 20 '15 at 17:23