DateFormatter returning nil for valid Date in format "dd/MM/yyyy" [duplicate] - ios

I have the below code on playground and app and DateFormatter are returning nil just for the date "1990-10-21":
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd"
let date1 = dateFormatter.date(from: "1990-10-20") // "Oct 20, 1990 at 12:00 AM"
let date2 = dateFormatter.date(from: "1990-10-21") // nil
let date3 = dateFormatter.date(from: "1990-10-22") // "Oct 22, 1990 at 12:00 AM"
Testing with 1989, 2021... works fine...
Does anyone knows why or something about this? is it a bug?

There are three issues with your code. First YYYY is for YearForWeekOfYear. What you need is yyyy. Second you are not passing the time. Note that not all days starts at 12:00am (daylight savings transition dates). You can prevent the date formatter returning nil by setting its calendar or passing a valid time (12pm) along with the date. Third you should always set the locale to "en_US_POSIX" when using a fixed date format:
let dateFormatter = DateFormatter()
dateFormatter.calendar = .current
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
let date1 = dateFormatter.date(from: "1990-10-20") // "Oct 20, 1990 at 12:00 AM"
let date2 = dateFormatter.date(from: "1990-10-21") // "Oct 21, 1990 at 1:00 AM"
let date3 = dateFormatter.date(from: "1990-10-22") // "Oct 22, 1990 at 12:00 AM"

Related

How to convert string to date correctly? [duplicate]

This question already has answers here:
NSDateFormatter still parsing instead having incorrect format
(3 answers)
Closed 3 years ago.
Why does dateFormatter return the correct date from an invalid format string?
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2020/////07////////10") //"Jul 10, 2020 at 12:00 AM"
I will say more - it also works unexpectedly
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dotDate = dateFormatter.date(from: "2020....07...10") // Optional(2020-07-09 21:00:00 +0000)
let commaDate = dateFormatter.date(from: "2020,,,,07,,,,10") // Optional(2020-07-09 21:00:00 +0000)
My version is probably the issue in internal implementation on Apple side and comparison with the ASCII code table, where the codes of these characters (,,-,.,/) are in order (from 44 to 47)
This is probably a part of their algorithm.
If you want the formatter to return nil in this case you can change the dateFormat to:
dateFormatter.dateFormat = "yyyy/MM/dd"
Results
let date = dateFormatter.date(from: "2020/////07////////10") // nil
let date2 = dateFormatter.date(from: "2020/07/10") // Jul 10, 2020 at 12:00 AM"
let date3 = dateFormatter.date(from: "2020.07.10") // Jul 10, 2020 at 12:00 AM"
I did run your code and it gives nil
Whereas if I enter correct date format then I receive the output
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2020-7-10")
print(date) // Optional(2020-07-10 00:00:00 +0000)

Getting date from DateFormatter without delimiter(/ or -) returns wrong date:

Getting date from DateFormatter without delimiter returns the wrong date,
As per our requirement, we cannot use / or - in our date formatter string.
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.dateFormat = "ddMMyyyy"
dateFormatter.date(from: "1112987")
this returns "Nov 1, 2987 at 8:00 AM", however it should be "Dec 11 0987"
Another Example:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMddyyyy"
dateFormatter.date(from: "1131987") // "Jan 13, 1987 at 12:00 AM"
This should return nil as November 31st is a invalid date
Please suggest any solution
You have to update your date format from ddMMyyyy to MMddyyyy.
Your code :-
dateFormatter.dateFormat = "ddMMyyyy"
New code :-
dateFormatter.dateFormat = "MMddyyyy"
Also, you have to set 4 digit year instead of 3 digit.
Your code :-
dateFormatter.date(from: "1112987")
New code :-
dateFormatter.date(from: "11120987"
If you get any issue post your reply.

Swift DateFormatter returning wrong date

I have a issue with getting the correct date in the following case
let dateString = "May 2, 2018 at 3:31 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy 'at' HH:mm a"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: dateString)!
I am getting "May 2, 2018 at 8:31 AM" as the date, which is wrong
When the same date is used with a different dateformatting string i get the correct date
let dateFormatter3 = DateFormatter()
dateFormatter3.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter3.locale = Locale(identifier: "en_US_POSIX")
dateFormatter3.timeZone = TimeZone(identifier: "UTC")
let date3 = dateFormatter3.date(from: "2018-05-02T15:31:00")!
Date returned is "May 2, 2018 at 11:31 AM" which is correct.
I like to know what i am doing wrong in the first code block? Or is this a bug in the date formatter class ?
when hour is in am/pm(1~12) use hh, to hour in day(0~23) use HH.
This helps me format my dates:
I hope this would work for you:
let dateString = "May 2, 2018 at 3:31 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy 'at' hh:mm a"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: dateString)
print(date!)
Output:-

Date formatter returning nil value

I have this code working but now all of a sudden, it started crashing. I could not figure it out, any help would be appreciated.
let dateFormatter = NSDateFormatter()
dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.dateFromString("Thu, Jan 5, 2017 12:28 PM")
It is returning date as nil. Why? I want it to return proper date.
This looks like a possible duplicate question, but it is with specific details.
let dateformattor = NSDateFormatter()
dateformattor.dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateformattor.timeZone = NSTimeZone.localTimeZone()
let dt = "Thu, Jan 5, 2017 12:28 PM"
let dt1 = dateformattor.dateFromString(dt as String)
print(dt1) //print date and time UTC format.
Output :
2017-01-05 06:58:00 +0000
var dateFormatter = DateFormatter()
let dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.date(from: "Thu, Jan 5, 2017 12:28 PM")
OUTPUT :
Every time you work with time and date formatters remember to set locale and timezone!
Device with non-english locale will fail to parse "Thu, Jan 5, 2017 12:28 PM" as "Thu" and "Jan" are obviously english specific.
So:
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
or whatever time zone and locale you want to use

How to extract day, month and year (dd-MM-yyyy) from Date (2018-09-28 09:42:00 +0000 ) without time in Date format - iOS swift?

I want to get 2018-09-28 from 2018-09-28 09:42:00 +0000 in Date format. I
can extract the same in string format but I want to get this in Date format. Here is my sample code.
let date = Date(timeIntervalSince1970: (TimeInterval(timer/1000)))
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let myDate = df.string(from: date)
let updateDate = df.date(from: myDate)
//date - 2018-09-28
//updateDate - 2018-09-28 09:42:00 +0000
You can simply get your date string prefix 11 and insert noon time when parsing your string:
let str = "2018-09-28 09:42:00 +0000"
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "yyyy-MM-dd HH:mm"
if let date = df.date(from: str.prefix(11) + "12:00") {
print(date.description(with: .current))
}
// Friday, September 28, 2018 at 12:00:00 PM Brasilia Standard Time

Resources