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)
}
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.)
Using DateFormatter on a Unix timestamp
You can convert unixTimestamp
to date using Date(timeIntervalSince1970:)
.
let unixTimestamp = 1480134638.0
let date = Date(timeIntervalSince1970: unixTimestamp)
If you want to display date in string with specific formate than you can use DateFormatter
like this way.
let date = Date(timeIntervalSince1970: unixtimeInterval)
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" //Specify your format that you want
let strDate = dateFormatter.string(from: date)
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.
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"
Swift convert Unix timestamp to Date with timezone and save it to EKEvent
Date
s 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, EKCalendarEvent
s 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
.
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.
Convert Timestamp to Date with various formats Swift
As from Calendar object you can get if the date is today or yesterday.
based on that you can set String as you like.
I've created an example like you need.
below is code.
var yesterdayStr = "17-Jan-2018 10:23 AM"
var todayStr = "18-Jan-2018 10:23 AM"
var tomorrowStr = "19-Jan-2018 10:23 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy hh:mm a"
let yesterDaydate = dateFormatter.date(from: yesterdayStr)
let todayDate = dateFormatter.date(from: todayStr)
let tomorrowDate = dateFormatter.date(from: tomorrowStr)
let anotherDateFormatter = DateFormatter()
anotherDateFormatter.dateFormat = "hh:mm a"
if Calendar.current.isDateInYesterday(yesterDaydate!)
{
let yesterdayDisplayString = "Yesterday, " + anotherDateFormatter.string(from: yesterDaydate!)
}
if Calendar.current.isDateInToday(todayDate!) {
let todayDisplayString = "Today, " + anotherDateFormatter.string(from: todayDate!)
}
if Calendar.current.isDateInToday(tomorrowDate!) {
}
else {
let anotherDisplayString = anotherDateFormatter.string(from: tomorrowDate!)
}
and here is output,
Related Topics
Warning :-Presenting View Controllers on Detached View Controllers Is Discouraged
A Swift Example of Custom Views For Data Input (Custom In-App Keyboard)
How to Calculate the Size of a Folder
Create Uiimage With Solid Color in Swift
How to Tell Swiftui Views to Bind to Nested Observableobjects
Uibutton Action in Table View Cell
Trying to Handle "Back" Navigation Button Action in Ios
Sending Data With Segue With Swift
Anyobject Not Working in Xcode8 Beta6
How to Add an In-App Purchase to an iOS Application
Event Handling For iOS - How Hittest:Withevent: and Pointinside:Withevent: Are Related
Trying to Convert Firebase Timestamp to Nsdate in Swift
How to Make App Fully Working Correctly for Autorotation in iOS 6
Change Color of Certain Pixels in a Uiimage
How to Enable_Bitcode in Xcode 7
Error Opening iOS Simulator with iOS 8 Beta Version: "Unable to Boot the iOS Simulator"