Creating an era in fast
I am a fast ios middle tier developer, now developing an application where I need to use the epoch time which is generated from the current date and time. For this I have tried the following code
var date = NSDate()
var timestamp = (floor(date.timeIntervalSince1970 * 1000))
and get the Float value, something like 1411032097112.0 .. But in my case, I only want the whole part of that result. Is this the best way to achieve this, or is there another better solution?
thank
source to share
What about
var timestamp = UInt64(floor(date.timeIntervalSince1970 * 1000))
Edit:
As @MartinR points out in his answer, Int64
would be a better choice than Int
due to space constraints in 32-bit devices. UInt64
will be better since you get a time interval that will always be positive.
source to share
You can simply convert your floating point value to Int64
:
let date = NSDate()
let timestamp = Int64(date.timeIntervalSince1970 * 1000.0)
println(timestamp)
// 1411034055289
You should use Int64
or UInt64
instead Int
, because the latter is only 32-bit on 32-bit devices, which isn't big enough for the time in milliseconds.
source to share