Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-5967] DateFormatter returning nil for a valid Date string #48526

Closed
swift-ci opened this issue Sep 22, 2017 · 2 comments
Closed

[SR-5967] DateFormatter returning nil for a valid Date string #48526

swift-ci opened this issue Sep 22, 2017 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5967
Radar None
Original Reporter thealexandrelara (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version: Version 8.3.3 (8E3004b)

Swift 3

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee thealexandrelara (JIRA)
Priority Medium

md5: a97d39779f219b0c24daa79c250e6e55

Issue Description:

I was testing an app that has an UITextField where users enter a date, so I take the entered string and use a DateFormatter to generate a Date object.

The problem first occurred when I tried converting the string "10/15/2017" which returned a nil value. So I create a code to generate string from "01/01/2000" to "12/31/2020" and I realized that the problem is occurring in all years around October or November of each year.

I created a code to print all dates that returns a nil value:

import UIKit
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.locale = Locale(identifier: "en_US")
 
for day in 1...30 { // Generate the days
    for month in 1...12 { // Generate the months
        for year in 2000...2020 {
            if month == 2, day > 28 { // Check if month is february
               continue
            }
            str = "\(String(format: "%02d", month))/\(String(format: "%02d", day))/\(String(format: "%02d", year))"
            let date = dateFormatter.date(from: str)          
            if date == nil {
                print("\(str)")
            }
        }
    }
}

            

I also tried changing the dateFormat, or even the locale properties and I'm also getting nil for some entries.

dateFormatter.dateFormat = "MM/dd/yyyy"
@swift-ci
Copy link
Collaborator Author

Comment by Alexandre José Messias de Lara (JIRA)

This problem occurs if daylight saving time starts exactly on midnight, because the clock is adjusted one hour forward so the date is considered as nonexistent in timezones that adopt this practice of advancing clocks during a period of the year.

To solve this problem use isLenient property of DataFormatter, so it's going to use the next valid date:

dateFormatter.isLenient = true

@swift-ci
Copy link
Collaborator Author

Comment by Seyed Samad Gholamzadeh (JIRA)

Worked well for me thanks

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant