ISO8601DateFormatter accepts wrong input - swift

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?

Related

Swift ISO8601DateFormatter TimeZone

I receive a string timestamp from JSON formatted as follows: "2022-10-06T19:10:00.000Z"
I want to convert the string to my local timezone and possibly even reformat the whole thing to something more readable like "Oct 6 2022, 3:30:45 PM". I assume that I need to use both ISO8601DateFormatter() and DateFormatter() to make that happen?
When I run the code below I get "2022-10-07 02:10:00 +0000" which is tomorrow's date so obviously not my timezone.
How do I properly format this date/time stamp to my current timezone? Thanks!
let jsonDateString = "2022-10-06T19:10:00.000Z"
let dateFormatter = ISO8601DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.formatOptions = [.withYear, .withMonth, .withDay, .withTime, .withDashSeparatorInDate, .withColonSeparatorInTime]
let formattedDate = dateFormatter.date(from: jsonDateString)
print(formattedDate!)

I'm trying to enter a formatted date string as "yyyy-mm-dd H:i:s +0000" mySQL from Swift 5. Instead, I get "yyyy-mm-dd H:i:s 0000". How to add "+"?

#objc func datePickerDidChange(_ datePicker: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.medium
birthdayTextField.text = formatter.string(from: datePicker.date)
let compareDateFormatter = DateFormatter()
compareDateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
let compareDate = compareDateFormatter.date(from: "2013/01/01 00:01")
if datePicker.date < compareDate! {
birthdayContinueButton.isHidden = false
} else {
birthdayContinueButton.isHidden = true
}
}
You ask:
I get “yyyy-mm-dd H:i:s 0000”. How to add “+”?
Your date string in your code snippet is “2013/01/01 00:01”. There is neither “+0000” nor “0000” (nor seconds) there. So there is no + to add or remove.
FWIW, if you print a Date object, yes, it will print a date in the format of 2013-01-01 00:01:00 +0000. But that’s immaterial. That’s just how print will display Date on your console. But you do not care what debugging format print uses. All you care about is whether the DateFormatter correctly parsed the date (and how a separate DateFormatter will prepare the date string for display in the UI).
Bottom line, do no worry about how print displays Date objects. (If anything, the fact that it is including the timezone for debugging purposes is very useful.) Just make sure your date formatters are correctly parsing/generating date strings. And, when you want to display a date string in your UI, use a separate DateFormatter for that (but for that formatter, do not use dateFormat, but rather use dateStyle and timeStyle). For more information, compare the ”Working With User-Visible Representations of Dates and Times” and “Working With Fixed Format Date Representations” discussions in the DateFormatter documentation.

I want to format a date into a string, but its giving me nil, i want to use the date in a UITextField.text

// the creationdate is coming from an api call
var creationDate = "2020-11-04T16:46:59.439212Z"
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .none
formatter.dateFormat = "dd-MM-yyyy"
var creationDateFormattedInToDate = formatter.date(from:
creationDate)
print("date \(creationDateFormattedInToDate)")
So i want that date in the format 04-11-2020 and pass in a UITextField.text
You will need two formatters, one to parse the input date to a Date object and one to convert the date object to a string of the right format.
The input date seems to be a variant of a internet date/time so we use a ISO8601DateFormatter
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
The second formatter is a basic DateFormatter with a custom format
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = "dd-MM-yyyy"
And then we can use them like this
if let date = formatter.date(from: creationDate) {
someTextField.text = outputFormatter.string(from: date)
}
You will want to use one formatter for parsing the response from the server (which is in what’s called and “ISO 8601” or “RFC 3339” format), and another for preparing the string representation of the date in the UI.
Regarding the date formatter for parsing the server response:
Set the formatter’s locale to Locale(identifier: "en_US_POSIX").
The setting of the styles when parsing this date string are irrelevant if you’re going to set dateFormat.
When parsing the date from the string, set dateFormat to yyyy-MM-dd'T'HH:mm:ss.SSSSSSX.
If you ever plan on using this formatter for the reverse date-to-string conversion (for preparing date strings to be sent to the server) you might want to set the timeZone of the formatter to TimeZone(secondsFromGMT: 0).
Regarding the date formatter used to prepare the string representation of the date in your UI:
I would not advise ever using a fixed dd-MM-yyyy format in your UI. That might be natural for European users, but it may be unnatural to most US users, who generally expect to see month before the day.
I would suggest not using dateFormat for this second date formatter, but rather using a dateStyle (e.g. of .medium or .long). It results in a nice, localized, and natural reading date string.
If you insist in using dd and MM and yyyy in your UI, I’d localize it with setLocalizedDateFormatFromTemplate so that the day and the month appear in the logical order that this particular user would expect (month-followed-by-day for US users, day-followed-by-month for most other locales).
Thus:
let serverDateFormatter = DateFormatter()
serverDateFormatter.locale = Locale(identifier: "en_US_POSIX")
serverDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
serverDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSX"
let uiFormatter = DateFormatter()
uiFormatter.dateStyle = .medium // or uiFormatter.setLocalizedDateFormatFromTemplate("ddMMyyyy")
if let date = serverDateFormatter.date(from: creationDateString) {
let string = uiFormatter.string(from: date)
// use that `string` in your UI
}

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.

can't convert time stamp date to string in swift 4

I'm trying to convert a timeStamp string date to Date.
The result always returns nil.
func getDatefromTimeStamp (str_date : String , strdateFormat: String) -> String {
// stringDate '2018-01-01T00:00:00.000+03:00'
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone!
let date = dateFormatter.date(from: str_date)
dateFormatter.dateFormat = strdateFormat
let datestr = dateFormatter.string(from: date!)
return datestr
}
Your primary issue is that the format "yyyy-MM-dd'T'HH:mm:ssZ" does not match a string such as "2018-01-01T00:00:00.000+03:00". That string contains milliseconds but your format doesn't.
Update your format to "yyyy-MM-dd'T'HH:mm:ss.SSSZ".
That will fix the nil result.
Then you should clean-up your use of NSTimeZone. Just use TimeZone.
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
But there is no need to set the timezone when parsing this string because the string includes timezone information.
However, you may or may not want a timezone set when converting the resulting Date into the new String. It depends on what result you want.
Do you want the final string in UTC time (which is what you will get with your current code) or do you want the final string in the user's local time?
If you want the final string in the user's local time, don't set the timezone property at all. It will default to local time.
First of i highly recommend that you use guards instead of forces
Secondly why are you setting the date format twice? the first time is dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" then a few lines later resetting it with the parameter you passed in dateFormatter.dateFormat = strdateFormat. pick one and set it in the beginning - that may be causing your problem
Thirdly if that above is not the problem - make sure that your date is exactly in the necessary format if it is at all wrong it will return nil. even spaces and colons have to be perfect, i suggest using string builder to make sure they are consistant