Error while converting C# date string into swift3 date - swift

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

Related

Convert String to Date object returns nil [duplicate]

This question already has answers here:
Instantiated optional variable shows as nil in Xcode debugger
(2 answers)
Closed 1 year ago.
I'm having an issue where an attempt to turn a string into a date via a DateFromatter has produced nothing but 'nil' and I don't know where I'm going wrong. The code is as simple as can be:
let testDate = "2021"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
let date = formatter.date(from: testDate)
Note that this is much simpler than originally, the date I'm trying to format is actually:
"2021-05-01T01:00:00Z"
But I've stripped it right down to narrow down where the issue is.
As you can see from above, I've stripped down to a year, configured the DateFormatter with en_US_POSIX and used only the 'yyyy' as a format. This works in Playgrounds, but it doesn't work in my Xcode simulator (Which is in the US locale) or my own physical iPhone (set to UK locale). That being said, I've no idea why the locale would matter because the string in question is just a year - it'm not even getting a wrong year, just nil.
I'm not sure where I'm going wrong.
Xcode Debugger has a bug as you can see in this post that it will show optional dates as nil even when parsing the date succeeds. If you print the optional date you will see the resulting date.
please use correct format like this for date string:
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
instead of :
formatter.dateFormat = "yyyy"
each date string with specific formate like below code:
let testDate = "2021"
formatter.dateFormat = "yyyy"
or
let testDate = "2021-05-01T01:00:00Z"
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
for more detail please see this link: https://stackoverflow.com/a/52297497/5140621

ISO8601DateFormatter accepts wrong input

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?

Converting string to date returns nil yyyMMdd

I am trying to convert my string to a date using a date formatter.
let timeSTR = "19740707"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let date = dateFormatter.date(from:timeSTR)
i am getting nil in the date object. any other timeSTR for example "19740706" or "19740705" or any other option will work ...
we found that if we will add the following command
dateFormatter.timezone = Timezone(identifier:"UTC")
it's fix the problem ...
more similar date that will return nil are: 19560603 , 19440401, 19400601
any one have an idea what wrong with those values???
Adding Print screen from my Playground with value 19740707:
Adding Print screen from my Playground with value: "19740706"

How to properly convert date into NSDate? (Unexpectedly found nil while unwrapping an Optional value) [duplicate]

This question already has an answer here:
DateFormatter doesn't return date for "HH:mm:ss"
(1 answer)
Closed 3 years ago.
I'm pulling in a contest end date from remote config (a string), and trying to cast it as an NSDate to use in a timer.
let contestEndDateFormatter = DateFormatter()
contestEndDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
contestEndDate = contestEndDateFormatter.date(from: contestEndDateFromRemoteConfig)! as NSDate
As you can see in the image, the string "2019-05-29 23:59:59" is definitely in the variable "contestEndDateFromRemoteConfig" (so it's not an issue with remote config); however, I'm obviously doing something wrong because it throws the error "Unexpectedly found nil while unwrapping an Optional value."
I believe the dateFormat is correct and matches the string (which was the suggestion from similar stackoverflow questions), so that shouldn't be the issue either.
Can anybody see why this would be throwing an error?
Thanks in advance for any direction!
Try this:
var date = "2019-05-29 23:59:59"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let formattedDate = dateFormatter.date(from: date)
if let formattedDate = formattedDate {
print(formattedDate)
}

Parse yyyy:MM:dd:hh:mm:ss using DateFormatter in Swift

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.