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)
DateFormatter returns previous day
(1 answer)
Closed 6 months ago.
I have Swift 5 and such date string:
let dateString = "2019-11-09T08:14:09.361404Z"
and two approaches to convert it to date:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"
let date = dateFormatter.date(from: dateString)
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withFractionalSeconds, .withInternetDateTime]
let date = dateFormatter.date(from: dateString)
as result:
first case: 2019-11-09 05:14:09 +0000
second case: 2019-11-09 08:14:09 +0000
Dates differ by 3 hours (just like my timezone differs from GMT). Setting the timezone to my current both formatters doesn't affect the results.
Could you please explain difference and which result is correct?
Related
This question already has answers here:
incorrect string to date conversion swift 3.0
(2 answers)
Closed 1 year ago.
I have to change the format of the date from dd/MM to dd/MM/yyyy and I use this code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM"
let date = dateFormatter.date(from: "28/07")
dateFormatter.dateFormat = "dd/MM/yyyy"
let stringDate = dateFormatter.string(from: date ?? Date())
The output is: 28/07/2000. What is wrong?
If you want to add current year, use this:
let date = Date()
let calender = Calendar.current
let components = calender.dateComponents([.year], from: date)
let year = components.year
and add this 'year' after your date string. Then you will have a date and current year.
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 1 year ago.
I stuck to convert string to date, my date string is
"2021-03-25T06:35:36.372245"
Unable to convert it into date formate. I used the code but unable to convert, i got nil.
My code is
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss.ZZZZZZ" ( also tried SSSSSS)
let dte = dateFormatter.date(from: dateTime)
but unable to parse it. Kindly help me out.
You forget '' arround T
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
Do this
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
let dte = dateFormatter.date(from: dateTime)
This question already has answers here:
Convert date string swift
(3 answers)
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 2 years ago.
Here is my code to convert but I had a nil value. Any help is appreciated
func serverToLocal(date:String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "GTM")
let localDate = dateFormatter.date(from: date)
return localDate
}
And he is my call:
let res = self.serverToLocal(date: "2015-06-18T17:02:02.614Z")
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 2 years ago.
I am working in Swift language. I don't know the date format for this 2020-07-23T00:17:06.000Z.
I want to convert Date to String by following that format & vice versa.
How can I do? Thanks in advance...
it is iso8601
let string = "2020-07-23T00:17:06.000Z"
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = dateFormatter.date(from: string)
This question already has answers here:
How to Convert UNIX epoch time to Date and time in ios swift
(3 answers)
Closed 2 years ago.
I get a unix timestamp from server
1589275703283
when I tried to convert it from this website: https://www.epochconverter.com
I get the value of Tuesday, 12 May 2020
and I'm using this code to convert this timestamp to date that I can read
func timeStringFromUnixTime(timestamp: Int64) -> String {
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd, MMMM yyyy"
dateFormatter.timeZone = TimeZone(identifier: NSTimeZone.default.identifier)
let localDate = dateFormatter.string(from: date)
return localDate
}
but it passed me 22 February 52332, which is obviously wrong
Could anyone help me where the problem come from?
thanks in advance
The timestamp 1589275703283 is way too big for a "normal" UNIX timestamp. It's in milliseconds instead of seconds (which epochconverter.com is smart enough to detect; it's printing "Assuming that this timestamp is in milliseconds"). So you need to divide your timestamp by 1000:
let date = Date(timeIntervalSince1970: TimeInterval(timestamp) / 1000)