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:
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:
Example (Swift 3):
or as a string:
And while are are at it, the reverse conversion from ticks to
Date
would beExample (Swift 3):
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.
Usage:
Cheers
In Swift:
if you want to makeit as global variable see below code:
Then, you can call it
The *1000 is for miliseconds, if you'd prefer, you can remove that. If keep it as an NSTimeInterval.
In Objective C:
If your want to declare as symbolic constant see below code:
Then Call it like this:
Or create a global method:
Have a Note: The 1000 is to convert the timestamp to milliseconds. You can remove this if you prefer your timeInterval in seconds.