How to Convert Unix Timestamp into Swift Nsdate Object

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

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.

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



Leave a reply



Submit