I would like to parse Year:Month:Day:Hour:Minute:Second (e.g. 2017:01:01:23:59:59) in Swift. I am using the following dateFormat: yyyy:MM:dd:hh:mm:ss though the date formatter returns nil when given a string:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy:MM:dd:hh:mm:ss"
dateFormatter.date(from: "2017:01:01:23:59:59") // nil
NSDateFormatter.com says there is something invalid in either my date or format. What am I doing wrong?
I believe your format is incorrect. It should be yyyy:MM:dd:HH:mm:ss instead. HH for hours.
Related
When parsing an input datetime string with the matching format, I am still getting nil back from DateFormatter in swift.
Input string (copied at runtime from variable) and confirmed that is the string running the call with Postman.
"2020-06-30T14:41:33.9"
Parsing method
static func FormatDateTimeString(dateTimeString: String) -> String {
let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = "YYYY-MM-DDThh:mm:ss.s"
let date = inputDateFormatter.date(from:dateTimeString) //date is always nil
let outputDateFormatter = DateFormatter()
outputDateFormatter.dateFormat = "MMM d, yyyy HH:mm"
return outputDateFormatter.string(from: date!)
}
I have also tried "YYYY-MM-DD'T'hh:mm:ss.s" and "YYYY-MM-DDThh:mm:ss" but none seem to work.
EDIT: Trying the format suggested below I am still getting nil.
EDIT: As Suggested from the comments I am still getting nil. Format below is: inputDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
EDIT: Still getting null using the following config
Use this format:
"yyyy-MM-dd'T'HH:mm:ss.S"
Demo
let dateTimeString = "2020-06-30T14:41:33.9"
let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
let date = inputDateFormatter.date(from: dateTimeString) // "Jun 30, 2020 at 2:41 PM"
I want to parse a date of the format yyyyMM to a date (e.g. 202007 should convert to July 2020). This works fine using DateFormatter - also when sending wrongly formatted input to it:
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMM"
let result = formatter.date(from: "***")// = nil
But after reading Apple's documentation for DateFormatter, I tried changing to ISO8601DateFormatter instead due to this phrase:
When working with date representations in ISO 8601 format, use
ISO8601DateFormatter instead.
However, I cannot make it work properly when sending invalid input to it:
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withMonth, .withYear]
let result = formatter.date(from: "***")//= 2000-01-01 00:00:00
Why does formatter.date return a date an not nil when sending a wrongly formatted string to it? Are there any way to make to above code return nil instead?
This question already has answers here:
Instantiated optional variable shows as nil in Xcode debugger
(2 answers)
Closed 2 years ago.
I have a problem with converting value from String to Date in Swift.
It always returns nil. What am I doing wrong?
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2019-05-03")
Here is what is happening in the debugger in Xcode
Update:
It would appear that it is a bug in Xcode. Trying your code in the viewDidLoad and setting a breakpoint causes the lldb description of the date to be nil, however it correctly prints the expected value out.
You can see more about the bug at this SO post, thanks to OOPer for the link.
Currently this bug is still occurring in Xcode 11.2-beta
A couple of points
Use .dateFormat instead of .format
Use the correct quotation marks " instead of ”, also you should remove the space from your date format string.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2019-05-03")
It does not have a nil value. Use .dateFormat instead of .format It's a simple as this:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2019-05-03")
// Optional(2019-05-02 23:00:00 +0000)
I cannot find an equivalent answer that resolves for me eg
Please see the code. I'm formatting an incoming string (item[9]) downloaded from a webservice.
var cal_end_date: Date!
var dateFormatter = DateFormatter()
let tempEndDate = item[9]
print("item[9]", item[9]) // 20190331
print("tempEndDate", tempEndDate) //20190331
dateFormatter.dateFormat = "YYYYMMDD"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone.autoupdatingCurrent
cal_end_date = dateFormatter.date(from:tempEndDate)!
print(#function, "cal_end_date:", cal_end_date!) //2019-01-31 05:00:00 +0000
The end date has the wrong month! I have confirmed this result by running the code in a playground with a fixed date. Am I doing something wrong here? Has something changed in Swift 5?
Thanks
You can always use nsdateformatter.com to check if your dateFormat for your formatter is correct (next to the Examples check Reference which shows you what each letter/letters represent).
In your case, you have to be carefull on dateFormat's case sensitive. Days and years are represented by small letters
dateFormatter.dateFormat = "yyyyMMdd"
In one of my app i am getting date from an RESTful api as "04112017182149".
I tried to convert it into swift date for my internal user as shown in below snippet.
let receivedDate = "04112017182149"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
print((dateFormatter.date(from: receivedDate))!)
It is throwing a fatal error as Unexpectedly found nil while unwrapping optional value.
I had tried to change date formatter string as "dd-MMM-yyyy HH:mm:ss" but no use.
The correct date format is
ddMMyyyyHHmmss