Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current date in milliseconds

Can any one give me an idea how to get the current date in milliseconds?

like image 970
siva Avatar asked May 27 '11 09:05

siva


People also ask

How do you convert timestamps to 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 .

How do you convert epoch time to milliseconds?

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.

How do you read time in 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.


1 Answers

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 
like image 55
Pawel Avatar answered Sep 29 '22 22:09

Pawel