How to Convert Unix Epoch Time to Date and Time in iOS Swift

Swift convert unix time to date and time

To get the date to show as the current time zone I used the following.

if let timeResult = (jsonResult["dt"] as? Double) {
let date = NSDate(timeIntervalSince1970: timeResult)
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle //Set time style
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle //Set date style
dateFormatter.timeZone = NSTimeZone()
let localDate = dateFormatter.stringFromDate(date)
}

Swift 3.0 Version

if let timeResult = (jsonResult["dt"] as? Double) {
let date = Date(timeIntervalSince1970: timeResult)
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.medium //Set time style
dateFormatter.dateStyle = DateFormatter.Style.medium //Set date style
dateFormatter.timeZone = self.timeZone
let localDate = dateFormatter.string(from: date)
}

Swift 5

if let timeResult = (jsonResult["dt"] as? Double) {
let date = Date(timeIntervalSince1970: timeResult)
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.medium //Set time style
dateFormatter.dateStyle = DateFormatter.Style.medium //Set date style
dateFormatter.timeZone = .current
let localDate = dateFormatter.string(from: date)
}

How to Convert UNIX epoch time to Date and time in ios swift

update: Xcode 8.2.1 • Swift 3.0.2 or later

You need to convert it from milliseconds dividing it by 1000:

let epochTime = TimeInterval(1429162809359) / 1000
let date = Date(timeIntervalSince1970: epochTime) // "Apr 16, 2015, 2:40 AM"

print("Converted Time \(date)") // "Converted Time 2015-04-16 05:40:09 +0000\n"

Convert Unix timestamp to date object swift 5

Cast the unix time stamp to a Double and call Date(timeIntervalSince1970:).

(Note: That converts the time stamp to a date. But how it is represented visibly, in the console or any other form of string output, is a completely different matter.)

How to transform a Epoch Unix Timestamp in milliseconds to a Date in swift

Please try this one :

let val = 1492495200000;
var date = Date(timeIntervalSince1970: (Double(val) / 1000.0));
print(date);

Well your value is time interval in milliseconds. This is reason why you should do division by 1000, before you will do date convert.

Swift convert unix time to date and time gives incorrect year

Divide timestamp by 1000, because Date uses seconds and the timestamp was generated with milliseconds and use TimeInterval instead of Double.

    let dateVal = TimeInterval(1598859638000) / 1000.0


let date = Date(timeIntervalSince1970: TimeInterval(dateVal))
print(date, "date", date.timeIntervalSince1970)


let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.init(abbreviation: "en")
dateFormatter.locale = Locale.init(identifier: "en")
dateFormatter.dateFormat = "E MMM d yyyy"
let outputDate = dateFormatter.string(from: date)
print(outputDate) // Mon Aug 31 2020

I guess your timestamp is generetd in a Java / Kotlin Applicatin. In java,
(new Date(1598859638000l)) yields Mon Aug 31 2020.

Swift convert Unix timestamp to Date with timezone and save it to EKEvent

Dates represent instants/points in time - "x seconds since a reference point". They are not "x seconds since a reference point at a location", so the timezone is not part of them. It makes no sense to "set the timezone of a Date", the same way it makes no sense to "set the number of decimal places of a Double".

It seems like you actually want to store a EKCalendarEvent. Well, EKCalendarEvents do have a timezone, because they are events that occur at a particular instant/day (occurrenceDate), in some timezone (timeZone). So you just need to set the timeZone property of the EKEvent, rather than the Date.

Unix Epoch time to Date returns strange results

Your creationDate is in milliseconds, not seconds. You need to divide by 1000.

let postDate = Date(timeIntervalSince1970: feeds[indexPath.row].creationDate / 1000)

Get wrong date value after converting unix time stamp

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)

Swift - How can I convert UNIX time to weekday with an option to offset?

You do not need a DateFormatter. Use Calendar.

func unixTimeToWeekday(unixTime: Double, timeZone: String, offset: Int) -> String {
if(timeZone == "" || unixTime == 0.0) {
return ""
} else {
let time = Date(timeIntervalSince1970: unixTime)
var cal = Calendar(identifier: .gregorian)
if let tz = TimeZone(identifier: timeZone) {
cal.timeZone = tz
}
// Get the weekday for the given timezone.
// Add the offset
// Subtract 1 to convert from 1-7 to 0-6
// Normalize to 0-6 using % 7
let weekday = (cal.component(.weekday, from: time) + offset - 1) % 7

// Get the weekday name for the user's locale
return Calendar.current.weekdaySymbols[weekday]
}
}

print(unixTimeToWeekday(unixTime: 1530676380, timeZone: "America/New_York", offset: 0))
print(unixTimeToWeekday(unixTime: 1530676380, timeZone: "America/New_York", offset: 1))

Output:

Tuesday

Wednesday

since 1530676380 is 2018-07-03 23:53:00 EDT (Tuesday).



Related Topics



Leave a reply



Submit