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)
Related
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?
I have date coming from API shown below. These dates are for different countries.
dateTime = "2022-03-04T14:30:00-08:00"
I need to convert this use it both as Date and as String. But I do not know if date and time I am converting are correct. I am using following code:
To Convert String from API to Date:
extension String {
var CommonDateFormat: Date? {
get {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter.date(from: self)
}
}
}
To Convert Date to String:
extension Date {
func getDateAccoringTo(format: DateFormat ) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format.rawValue
dateFormatter.timeZone = .current
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
return dateFormatter.string(from: self)
}
}
enum DateFormat: String {
case ddmmyyyy = "dd/MM/yyyy"
case mmddyyyy = "MM/dd/yyyy"
case mmmd_yyyy = "MMM d, yyyy"
case llll_yyyy = "LLLL ,yyyy"
case TIME = "HH:mm:ss"
case day = "dd"
}
When I try to get day from date it always gives next day date. For example if date = "2022-03-04T14:30:00-08:00" then if I try to get day using code below
date?.getDateAccoringTo(format: .day)
This returns 5 not 4
date?.getDateAccoringTo(format: .TIME)
This returns time 00:00:00
Am I missing something important which is leading to these values?
Also, if I am missing something in my question kindly let me know so that I can improve it.
The given string
let dateTime = "2022-03-04T14:30:00-08:00"`
is a standard ISO8601 formatted date string. It can be converted to Date with
let formatter = ISO8601DateFormatter()
let date = formatter.date(from: dateTime)!
At this specific point in time it is
14:30 on Friday, March 4 in Denver, CO, USA
22:30 on Friday, March 4 in London, UK
06:30 on Saturday, March 5 in Tokyo, Japan
Now let's see how Xcode displays dates.
print displays Date instances always in UTC indicated by +0000 which is the London time zone unless you print(date.description(with: .current), this displays the date in the local time zone.
In a Xcode Playground the result area displays Date instances in the local time zone except in print lines.
Last point to consider is that DateFormatter converts Date to String in the local time zone if no time zone is specified.
Keeping this behavior in mind you get the next day if you convert the date to string with DateFormatter but without specifying the time zone and your local time zone is greater than or equal to +01:30.
And you get the time 00:00 if you convert the date to string with DateFormatter but without specifying the time zone and your local time zone is exactly +01:30 which is a pretty unusual time zone by the way.
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)