How to convert Unix timestamp into Swift NSDate object?
Try this:
var date = NSDate(timeIntervalSince1970: timeInterval)
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
.
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.)
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 timestamp to NSData object?
You are confused. What you're sending to the peripheral is an integer number of seconds since 1970. That's a reasonable way to send a Unix timestamp, but it is not a time in 24 hour format, it is an integer.
You will need to change your code to use a uint64_t or uint32_t since Unix timestamps are much larger numbers than will fit in a 32 bit integer. (I suggest using uint64_t.)
(See @DonMag's comment for sample timestamp values, like 1491580283)
How you display that time once it is received by the peripheral is a separate question, and the question that you should really be asking.
Note that you may run into problems sending an int as binary data if the peripheral has different "endian-ness" than your iOS device. you might want to convert the timestamp integer to a string and send that in order to avoid the endian-ness problem.
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)
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"
Getting date and time from unix timestamp
Usually unix timestamps are in milliseconds, but Date uses seconds. You might need to divide your timestamp by 1000.
Create NSDate from Unix timestamp
Try this instead:
NSNumber *startTime = channelJson[@"broadcastStartedTime"];
channel.startDate = [NSDate dateWithTimeIntervalSince1970:[startTime doubleValue]];
Your value is trapped in a pointer type of NSNumber
. The dateWithTimeIntervalSince1970
method is expecting a primitive NSTimeInterval
(which, under the covers, is a double
).
Related Topics
How to Move a Model and Generate Its Collision Shape at the Same Time
Accessing and Manipulating Array Item in an Environmentobject
Firestore Security Rules Breaking with Update Rule
Cfdictionary Get Value for Key in Swift3
How to Make a Function with a Loop Asynchronous in Swift
Randomizing Node Movement Duration
How to Group an Array of Image Urls Together in Firebase Realtime Database? (Swift)
How to Use a Specific Gmt for a Function Which Will Be Recognised by Other Time Zones
Contacts Not Recognized When Body Is Changed from Circle to Rectangle
Why Reloaddata Is Showing Error for My Tableview Inside Viewcontroller
Change Document in Open Window in MACos App
How to Execute Different Implementation of a Method of a Generic Struct Based on the Generic Type
Access Custom Object Property While Iterating Over Dictionary