What is the correct date format for this date-as-a-string? [duplicate] - ios

This question already has answers here:
How to convert "2017-07-11T06:52:15.948Z" in to like "JUL, 7 2017" in swift
(1 answer)
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
The back end is providing me with this date-as-a-string: 2021-09-10T12:57:01.671Z
I need to convert that string to a Date using the iOS DateFormatter; and to do so I need to set the formatter's dateFormat property.
I have tried all sorts of combinations, with no luck. Specifically I am struggling with the .671Z part.
What is the correct date format to use?

You need "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" as date format
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
guard let date = dateFormatter.date(from: self) else {
// fallback if date is not in correct format
return nil
}

Related

Formatting a date from a string in swift [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 have the following date coming from a server 2019-09-05T10:37:49.494Z as a string and i need to parse this and convert it to a format like this Fri September 13,2019 12:36 AM and back to a string again:
i found multiple question links but none of them are working for me Question One
Question Two
it tried doing this:
let dateFormatterGet = DateFormatter()
let dateFormatterPrint = DateFormatter()
var rawDate = "2019-09-05T10:37:49.494Z"
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatterPrint.dateFormat = "E, d MMM yyyy HH:mm"
var formattedDate = "Error Formatting date"
if let date = dateFormatterGet.date(from: rawDate) {
formattedDate = dateFormatterPrint.string(from: date)
print("Formatted Date : \(formattedDate)")
}else {
print("There was an error decoding the string")
}
this fails printing the error message, what am i doing wrong and how can i fix it?
You are almost there.
A small tip playing with (NS)DateFormatter put the dateFormat above/under the date string.
yyyy-MM-dd'T'HH:mm:ssZ
2019-09-05T10:37:49.494Z
Then, add "spaces" to align and separate them.
yyyy - MM - dd 'T' HH : mm : ss Z
2019 - 09 - 05 T 10 : 37 : 49 . 494Z
^ ^^^
I highlighted the missing ones. You need to tell the (NS)DateFormatter through the dateFormat how to interpret theses additional characters.
Let's check the documentation.
It's
Fractional Second - truncates (like other time fields) to the count of letters. (example shows display using pattern SSSS for seconds value 12.34567)
So using yyyy-MM-dd'T'HH:mm:ss.SSSZ should interpret them and fix your issue.
That's how you fix your issue. And it explained your error.
But since as pointed by #Zombie it's using a ISO format, use if available the ISO8601DateFormatter if possible (iOS10+)
If in future cases you don't have an ISO something format, you can use theses tips ;)
The format you provided seems like an iso 8601 date for this reason I would suggest using the ISO8601DateFormatter
You can specify the options to match your string
here is an example
let dateString = "2019-09-05T10:37:49.494Z"
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withDashSeparatorInDate,
.withFullDate,
.withFullTime,
.withFractionalSeconds,
.withColonSeparatorInTime
]
// "Sep 5, 2019 at 12:37 PM"
let date = formatter.date(from: dateString) ?? Date()
//"Thursday, September 5, 2019 at 12:37:49 PM"
let formattedDate = DateFormatter.localizedString(
from: date,
dateStyle: .full,
timeStyle: .medium
)
The problem is you don't tell dateFormatterGet how to parse milliseconds. Modify the dateFormat to:
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

Convert current date into yyyy-MM-ddTHH:mm:ss.fffffffzzz format [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 4 years ago.
Code:
let format1 = DateFormatter()
format1.dateFormat = "yyyy-MM-ddTHH:mm:ss.fffffffzzz"
format1.timeZone = TimeZone(abbreviation: "UTC")
print(format1.string(from:Date()))
I am trying to get date string in this format(2018-12-19T12:30:00.000
+11:00) but i got date(2018-12-12) only.what is wrong with mycode.any help will be appricated.thanks in advance
You are not using proper format, please try this will help you.
let format1 = DateFormatter()
format1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZZ"
format1.timeZone = TimeZone(abbreviation: "UTC")
format1.locale = Locale(identifier: "en_US_POSIX")
print(format1.string(from:Date()))
This will print following output:
2018-12-24T14:16:11.011 +0000

Convert from 2018-10-23T06:01:10.806Z to 10-May-2018 and a seperate string for time [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 4 years ago.
have to extract a separate date and time from the same string i.e)2018-10-23T06:01:10.806Z, the date must be in the format of 10-May-2018 and the time must be in 12 hours format i.e) 08:00 PM
Try this
func formatDateString(dateString: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
return dateFormatter.date(from: dateString)
}
You get the Date object, do whatever you can do whatever you wanted using that.

Date From Timestamp Becomes Wrong [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to convert timestamp to Date. But it returns wrong date.
I have a time stamp
1524637838000.0
Which returns 25-04-2018 12:00 as per this online converter
But I get wrong date when convert using my code
let date = Date(timeIntervalSince1970:1524637838000.0)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
let myDate = dateFormatter.string(from: date)
print("Date is = ",myDate)
I get
07-11-50283 12:03
as result. Is there anything wrong with my code?
your timestamp 1524637838000.0 in milliseconds, it should be in seconds, so your date initialization should be:
let date = Date(timeIntervalSince1970:(1524637838000.0/1000))

NSDateFormatter for datetime [duplicate]

This question already has answers here:
How do I get an ISO 8601 date on iOS?
(12 answers)
Closed 7 years ago.
I have a string which is coming from an API that I need to convert into a NSDate but I'm uncertain which dateFormat to use for NSDateFormatter.
let openedAt = "2015-06-30T12:34:00.000-04:00" // coming from API
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd..." // not sure what format to use here
if let date = formatter.dateFromString(openedAt) {
println(date)
} else {
println("NOPE!")
}
try
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.

Resources