How to get 18-digit current timestamp in Swift?
Solution 1
You seem be looking for what DateTime.Ticks
is in C#, i.e. the
time since 0001-01-01 measured in 100-nanosecond intervals.
The code from your provided link Swift: convert NSDate to c# ticks can be translated to Swift easily:
// Swift 2:
extension NSDate {
var ticks: UInt64 {
return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)
}
}
// Swift 3:
extension Date {
var ticks: UInt64 {
return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)
}
}
Example (Swift 3):
let ticks = Date().ticks
print(ticks) // 636110903202288256
or as a string:
let sticks = String(Date().ticks)
print(sticks)
And while are are at it, the reverse conversion from ticks to Date
would be
// Swift 2:
extension NSDate {
convenience init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
// Swift 3:
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
Example (Swift 3):
let date = Date(ticks: 636110903202288256)
Solution 2
Here is a solution that I find very elegant. I extended NSDate (although it's just Date now in Swift 3) to include a toMillis() func which will give you the Int64 value of the date object that you're working with.
extension Date {
func toMillis() -> Int64! {
return Int64(self.timeIntervalSince1970 * 1000)
}
}
Usage:
let currentTimeStamp = Date().toMillis()
Cheers
Solution 3
In Swift:
if you want to makeit as global variable see below code:
var Timestamp: String {
return "\(NSDate().timeIntervalSince1970 * 1000)"
}
Then, you can call it
println("Timestamp: \(Timestamp)")
The *1000 is for miliseconds, if you'd prefer, you can remove that. If keep it as an NSTimeInterval.
var Timestamp: NSTimeInterval {
return NSDate().timeIntervalSince1970 * 1000
}
In Objective C:
If your want to declare as symbolic constant see below code:
#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]
Then Call it like this:
NSString * timestamp = TimeStamp;
Or create a global method:
- (NSString *) timeStamp {
return [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
}
Have a Note: The 1000 is to convert the timestamp to milliseconds. You can remove this if you prefer your timeInterval in seconds.
Related videos on Youtube
The Cook
You clearly don't know who you're talking to, so let me clue you in: I am not in danger, Skyler. I am the danger. A guy opens his door and gets shot, and you think that of me? No! I am the one who knocks!
Updated on July 09, 2022Comments
-
The Cook almost 2 years
I want to get current timestamp like this:
636110767775716756
However, when I do :
NSDate().timeIntervalSince1970
It returns a value like this:
1475491615.71278
How do I access current time stamp ticks in the format I want? I check the dates from here:
-
vaibhav over 7 yearsexactly you need current date and time as string .. ?
-
The Cook over 7 yearsYes I need it as string, in 18 digit format.
-
Anya Shenanigans over 7 yearsWhat's the origin/form of that timestamp? The issue is that the
timeIntervalSince1970
's leading digit is currently1
, which doesn't really match with what you're looking for. -
The Cook over 7 yearsstackoverflow.com/questions/36620342/… This is what i am looking for but in Swift.
-
-
The Cook over 7 yearsThanks for the reply, however it returns only a 13-digit timestamp like this: 1475492283055. I need 18 digit.
-
The Cook over 7 yearsThis worked pretty good. I used this on NSDate instead of Date.