Swift: DateFormatter not able to parse date - ios

I've a specific time format which I want to use to generate a Date object.
This is the format:
Fri, 17 Mar 2017 08:42:00 +0100
Here is my Swift code that should create the data object:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
let d = dateFormatter.date(from: value)
The generation of the Date object with dateFormatter.date does always return nil. But I cant see what's wrong in my formatting string.
Can anyone see my error?

I have test it and works perfectly.
Could you test this code line to identify you Locale configuration?
print(Locale.current.identifier)
I only made a little change related to the Optional result that DateFormatter method returns
import Foundation
let the_date: String = "Fri, 17 Mar 2017 08:42:00 +0100"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
if let d = dateFormatter.date(from: the_date)
{
print(d)
}

Related

Convert string containing date to Date

I'm having issues converting a string to date on swift, maybe it is something obvious but I don't get it.
I'm trying to convert "Jan 18, 2022 04:39PM GMT" this string into a Date. My code looks like this:
let str = "Jan 18, 2022 04:39PM GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, YYYY hh:mma z"
let date = dateFormatter.date(from: str)
print(date)
And console shows: Optional(2021-12-19 16:39:00 +0000)
Any idea what's wrong in this formatter?
In addition to the Date being shown as an Optional, your format string appears to be wrong. "YYYY" should be "yyyy", so the whole line that assigns the formatter should be:
dateFormatter.dateFormat = "MMM d, yyyy hh:mma z"
That change yields the output
"Optional(2022-01-18 16:39:00 +0000)"
In addition, you should really force the calendar to Gregorian or iso8601, and set its locale to "en_US_POSIX:
An improved version of the date formatter could would look like this:
(from Leo's edit.)
let str = "Jan 18, 2022 04:39PM GMT"
let dateFormatter = DateFormatter()
dateFormatter.calendar = .init(identifier: .iso8601)
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "MMM d, yyyy hh:mma z"
if let date = dateFormatter.date(from: str) {
let dateString = dateFormatter.string(from: date)
print(dateString == str) // true
}
The code written for converting date is correct, also converted date is correct. But final result is optional so you are getting date like Optional(2021-12-19 16:39:00 +0000).
Also the date formatter is wrong.
So please unwrap the date to get actual date without optional.
dateFormatter.dateFormat = "MMM d, yyyy hh:mma z"
guard let convertedDate = date else {
return
}
print(convertedDate)

Parse ISO8601 in swift

I am parsing 2020-06-09T18:11:26.904Z and it returns this 09 Jun, 11:11 pm it should be 06:11 pm
I have tried this solution Parsing a ISO8601 String to Date in Swift but it also works like the same.
Here's my code.
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = df.date(from: dateTime) else {
return nil
}
df.dateFormat = "dd MMM, hh:mm a"
df.amSymbol = "am"
df.pmSymbol = "pm"
let stringFromDate = df.string(from: date)
return stringFromDate
I know I could ask it on the mentioned link but due to the new user I couldn't add comment on that.
You didn't set the timezone of the output date, so it's defaulting to your systems' timezone rather than UTC (which is what you were expecting).

DateFormater error only when running on iOS 13

I have a function to convert string to date format. This function works as expected on iOS 12 but on iOS 13. I get this error:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
Here is my code:
func ConvertDateAndTimeFormat2() {
let timeDate = "2019-09-24 15:00:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
var dateObj:Date!
dateObj = dateFormatter.date(from: timeDate)
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
var timeObj:Date!
timeObj = timeFormatter.date(from: timeDate)
timeFormatter.dateFormat = "HH:mm"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
let timef = "\(timeFormatter.string(from: timeObj!))"
let Date = "\(dateFormatter.string(from: dateObj!))"
}
Please change your dateformat and use "yyyy-MM-dd HH:mm:ss Z" and try.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
For more details please visit https://nsdateformatter.com/
If you are trying to convert string time which in 24 hours format (Ex 15:25:00 / HH:mm:ss) then enable your device 24-Hour Time option. It might solve the issue in IOS 13.3
You may find the option here
Settings -> General -> Date & Time
There are many issues with your code. There is no reason to create two Date objects from one string. Just parse the original string once. Then you can create your date string and time string with the desired formats from that one date.
You should also use the special locale of en_US_POSIX when parsing fixed format date strings. There is also no need to set a timezone when parsing the original date string. The string provides its own timezone. The +0000 means it is UTC time.
You may not want to provide a timezone when converting the date to your final strings either. Most likely you want strings in the user's locale timezone, not some hardcoded timezone.
Here's your code cleaned up a lot:
func convertDateAndTimeFormat2() {
let timeDate = "2019-09-24 15:00:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let dateObj = dateFormatter.date(from: timeDate) {
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") // Probably not needed
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:mm"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") // Probably not needed
let timef = timeFormatter.string(from: dateObj)
let datef = dateFormatter.string(from: dateObj)
print("Date: \(datef), time: \(timef)")
}
}
convertDateAndTimeFormat2()
Output:
Date: Tuesday, Sep 24, 2019, time: 18:00

Swift date from string always returns nil

I have a date that is a string that looks like this:
Apr 25 2018 12:00AM
What I am trying to do is convert this date format to yyyy-MM-dd and then convert it back to a string, I have a tried the following:
let formatter = DateFormatter()
formatter.dateFormat = "M dd yyyy h:mm A"
let SLAIssuedFinalGradingDate = formatter.date(from: tableDic["SLAIssuedFinalGradingDate"] as! String)
formatter.dateFormat = "yyyy-MM-dd"
let SLAIssuedFinalGradingDateString = formatter.string(from: SLAIssuedFinalGradingDate!)
But SLAIssuedFinalGradingDate always returns nil, what am I doing wrong?
Your dateFormat for Apr 25 2018 12:00AM is not right. You are using M dd yyyy h:mm A, but format would be MMM dd yyyy hh:mma.
Use this link to check date format.
Code Should be:
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MMM dd yyyy hh:mma"
let SLAIssuedFinalGradingDate = formatter.date(from: tableDic["SLAIssuedFinalGradingDate"] as! String)
formatter.dateFormat = "yyyy-MM-dd"
let SLAIssuedFinalGradingDateString = formatter.string(from: SLAIssuedFinalGradingDate!)
Your dateFormat string is incorrect. A DateFormatter will always return nil if the dateFormat string does not match the supplied string.
"MMM dd yyyy hh:mma"
This is what you're after. I suggest you review a DateFormatter cheat sheet and familiarise yourself with the symbols.

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

Resources