How do I get the current Unix time in milliseconds (i.e number of milliseconds since Unix epoch January 1 1970)?
Sep 29, 2018 - A protip by andreaspizsa about timestamp, js, javascript, unix time, posix time, and epoch time. Unix time (AKA epoch time) is used widely in Unix-like and many other operating systems and file formats. Unfortunately, there's a small discrepancy between the C-style time call and JavaScript's Date.
RichardRichardwill return the number of seconds since the epoch.
returns the seconds and current nanoseconds.
will give you the number of milliseconds since the epoch - current seconds plus the left three of the nanoseconds.
and from MikeyB - echo $(($(date +%s%N)/1000000))
(dividing by 1000 only brings to microseconds)
You may simply use %3N
to truncate the nanoseconds to the 3 most significant digits (which then are milliseconds):
This works e.g. on my kubuntu 12.04.
But be aware, that %N
may not be implemented depending on your target system.E.g. tested on an embedded system (buildroot rootfs, compiled using a non-HF arm cross toolchain) there was no %N
:
(And also my (non rooted) Android tablet has no %N
).
date +%N
doesn't work on OS X, but you could use one of
ruby -e 'puts Time.now.to_f'
python -c 'import time; print time.time()'
node -e 'console.log(Date.now())'
php -r 'echo microtime(TRUE);'
DateTime.utc_now() |> DateTime.to_unix(:millisecond)
wget -qO- http://www.timeapi.org/utc/now?s.N
date +%s000
Just throwing this out there, but I think the correct formula with the division would be:
splattneMy solution is not the best but worked for me.
date +%s000
I just needed to convert a date like 2012-05-05 to milliseconds.
jscottFor the people that suggest running external programs to get the milliseconds... at that rate, you might as well do this:
Point being: before picking any answer from here, please keep in mind that not all programs will run under one whole second. Measure!
Camilo MartinCamilo Martinthis solution works on macOS.
if you consider using a bash script and have python available, you could use this code:
If you are looking for a way to display the length of time your script ran, the following will provide a (not completely accurate) result:
As near the beginning of your script as you can, enter the following
basetime=$(date +%s%N)
This'll give you a starting value of something like 1361802943996000000
At the end of your script, use the following
echo 'runtime: $(echo 'scale=3;($(date +%s%N) - ${basetime})/(1*10^09)' | bc) seconds'
which will display something like
runtime: 12.383 seconds
Notes:
(1*10^09) can be replaced with 1000000000 if you wish
'scale=3'
is a rather rare setting that coerces bc
to do what you want. There are lots more!
I only tested this on Win7/MinGW... I don't have a proper *nix box to hand.
Here is how to get time in milliseconds without performing division. Maybe it's faster...
Update: Another alternative in pure bash that works only with bash 4.2+
is same as above but use printf
to get the date. It will definitely be faster because no processes are forked off the main one.
Another catch here though is that your strftime
implementation should support %s
and %N
which is NOT the case on my test machine. See man strftime
for supported options. Also see man bash
to see printf
syntax. -1
and -2
are special values for time.
The most accurate timestamp we can get (at least for Mac OS X) is probably this:
But we need to keep in mind that it takes around 30 milliseconds to run.We can cut it to the scale of 2 digits fraction, and at the very beginning compute the average overhead of reading the time, and then remove it off the measurement. Here is an example:
You can uncomment the echo commands to see better how it works.
The results for this script are usually one of these 3 results:
Using date and expr can get you therei.e.
Just expand on it to do whatever you want
I realise this does not give milliseconds since epoch, but it might still be useful as an answer for some of the cases, it all depends on what you need it for really, multiply by 1000 if you need a millisecond number :D
Simplest way would be to make a small executable (from C f.ex.) and make it available to the script.
kasperd(repeat from above) date +%N
doesn't work on OS X, but you could use also :
Perl (requires Time::Format module). Perhaps not the best CPAN module to use but gets the job done. Time::Format is generally made available with distributions.
Putting all together previous responses, when in OSX
you can do like
If you want a simple shell elapsed computation, this is easy and portable, using the above answer:
For alpine linux (many docker images) and possibly other minimal linux environments you can abuse adjtimex
:
adjtimex
is used to read (and set) kernel time variables. With awk
you can get the microseconds, with head
you can use the first 3 digits only.
I have no idea how reliable this command is.
Note: Shamelessly stolen from this answer