Swift gets Current timestamp (sec, millisecond)
It is often seen that a second-level timestamp is obtained in the form of an Int type direct * 1000来 MS, so that the final 3 digits are 0, and the results are not accurate enough. So there's another way to get a millisecond timestamp. Create a Date Extension with the following code
Extension Date {
///gets the current second-level timestamp-10-bit
var timestamp:string {let
timeinterval:timeinterval = Self.timeinte rvalSince1970 let
timeStamp = Int (timeinterval) return
"\ (TimeStamp)"
}
///Obtain the current millisecond timestamp-13-bit
var millistamp:string {let
timeinterval:timeinterval = self.timeintervalsince1970 let
millisecond = CLongLong ( Round (timeinterval*1000)) return
"\ (Millisecond)"
}
}
Use
Let millisecond = Date (). Millistamp
Print (the current millisecond timestamp is millisecond = =, millisecond) let
timeStamp = Date (). TimeStamp
Print (the current second-level timestamp is timeStamp = =, TimeStamp)
/**
output
The current millisecond timestamp is millisecond = 1524801851988
The current second-level timestamp is TimeStamp = = 1524801851
* *