Can any one give me an idea how to get the current date in milliseconds?
A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .
Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds.
To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.
There are several ways of doing this, although my personal favorite is:
CFAbsoluteTime timeInSeconds = CFAbsoluteTimeGetCurrent();
You can read more about this method here. You can also create a NSDate object and get time by calling timeIntervalSince1970 which returns the seconds since 1/1/1970:
NSTimeInterval timeInSeconds = [[NSDate date] timeIntervalSince1970];
And in Swift:
let timeInSeconds: TimeInterval = Date().timeIntervalSince1970
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With