How to convert String date to a different String date? [duplicate] - ios

This question already has answers here:
How can I convert string date to NSDate?
(18 answers)
Closed 5 years ago.
I want to convert December 20, 2016 to 12/20/16.
December 20 is in String format. Not too sure if I can use NSDateFormatter for this. I do not what MM/DD/YYYY to something like YYYY:DD:MM. I want to convert a String December 20, 2016, and get it to 12/20/16.

Yes you can use DateFormatter (without NS in Swift 3, the same applies for Date not NSDate). To properly parse your date string you need to use the date format "MMMM dd, yyyy" and set the date formatter locale to "en_US_POSIX"
let string = "December 20, 2016"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MMMM dd, yyyy"
if let date = formatter.date(from: dateString) {
print(date) // "2016-12-20 02:00:00 +0000\n"
formatter.dateStyle = .short
let stringFromDate = formatter.string(from: date) // "12/20/16"
}

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)

String to date conversion sometimes returns nil with DateFormatter

I just read over this post which details that you need to set a locale before the format before attempting to convert a given string back to a date.
I have the below code that follows that format but the conversion from string to date still seems to return nil though in the playground it seems to work fine.
// originalDate style is the .full style, "Friday, December 10, 2021 at 4:17:04 PM Pacific Standard Time"
let df: DateFormatter = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "MMM d, yyyy" // Sep 12, 2018 format'
let newDate = df.date(from: originalDate)
let newStringDate = df.string(from: newDate!) // throws found nil when unwrapping
What could be causing this?
Edit 1: I'm pulling a string type out of a sqlite db using SQLite.swift
Answer for your question (string to date is nil): While converting from string to date, you have to use the same format as String. I assume the originalDate from your comments. You have to change your code like this
let originalDate = "Friday, December 10, 2021 at 4:17:04 PM"
let df: DateFormatter = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "EEEE, MMMM d, yyyy 'at' h:mm:ss a"
let newDate = df.date(from: originalDate)
print(newDate)
If you again want string from date then you can again specify which format you want.
df.dateFormat = "MMM d, yyyy" // Sep 12, 2018 format'
let newStringDate = df.string(from: newDate!)
print(newStringDate)
Note: If you try to log the newDate value in the console using print, sometimes it shows nil. In that case, you can try 'po newDate' in the console.

Dateformatter returns different dates [duplicate]

This question already has answers here:
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 2 years ago.
Please check the code bellow:
func testDate() {
let calendar = Calendar.current
let dateEnd = calendar.date(byAdding: .day, value: 15, to: Date())
let df:DateFormatter = DateFormatter()
df.dateFormat = "MMMM dd, YYYY HH:mm"
df.timeZone = calendar.timeZone
df.timeZone = Calendar.current.timeZone
let startDateLabel = df.string(from: dateEnd!)
print(startDateLabel)
let min = df.date(from: startDateLabel)!
print(min)
}
Output
print1: March 12, 2020 14:48
print2: 2019-12-22 09:48:00 +0000
Use below dateFormat instead
df.dateFormat = "MMMM dd, yyyy HH:mm"
printing the date to console would give you the print2. It's printing the Date formats debug description. If you require a particular format you need to convert date into string with a DateFormatter. Check this link you need more help to get the desired date format.

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)

Swift `yyyy-MM-dd'T'HH:mm:ss.sssZ` string to date [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 3 years ago.
I'm formatting a date string to date object using below code. But for some date strings it works and for some it returns nil.
Format = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
"2019-04-02T09:47:24.055Z" Works
"2019-03-27T22:31:17.140Z" Doesn't work
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
let date = dateFormatter.date(from: "2019-03-27T22:31:17.140Z")
Your format is wrong. It should be:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Have a look at date time format here:
https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2019-04-02T09:47:24.055Z")
let date1 = dateFormatter.date(from: "2019-03-27T22:31:17.140Z")
print(">>>>>!!", date, date1)
Optional(2019-04-02 09:47:24 +0000) Optional(2019-03-27 22:31:17 +0000)
The code above works as expected.
use SSS instead of sss
for your case of
yyyy-MM-dd'T'HH:mm:ss.sssZ and 2019-04-02T09:47:24.055Z
output was :
Optional(2019-04-02 09:47:55 +0000) nil
look at minutes and seconds:)

Resources