Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How do I get the current Unix time in milliseconds (i.e number of milliseconds since Unix epoch January 1 1970)?

share|improve this question

9 Answers 9

up vote 99 down vote accepted


date +%s 

will return the number of seconds since the epoch.


date +%s%N

returns the seconds and current nanoseconds.


date +%s%N | cut -b1-13

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)

share|improve this answer
I wonder how many ms the cut adds :-) –  Kyle Brandt Jun 14 '10 at 16:23
I think seconds * 1000 will be fine :) –  Richard Jun 14 '10 at 16:34
Or if you want to do it all in the shell, avoiding the expensive overhead of an additional process (actually, we're avoiding the problem when the number of digits in %+s+N changes): echo $(($(date +%s%N)/1000)) –  MikeyB Jun 14 '10 at 16:38
It's the principle of the matter... avoid magic numbers and code what you actually mean. –  MikeyB Jun 14 '10 at 18:02
I think it's worth noting that the man asked for Unix, not Linux, and the current top answer (date +%s%N) doesn't work on my AIX system. –  Pete Oct 25 '11 at 16:52

You may simply use %3N to truncate the nanoseconds to the 3 most significant digits (which then are milliseconds):

$ date +%s%3N

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:

$ date +%s%3N

(And also my (non rooted) Android tablet has no %N).

share|improve this answer

date +%N doesn't work on OS X, but you could use ruby -e 'puts Time.now.to_f' or python -c 'import time; print time.time()'.

share|improve this answer
for completeness... node -e 'console.log(Date.now())' –  slf Oct 10 '13 at 18:06
Using PHP: php -r 'echo microtime(TRUE);' –  TachyonVortex Nov 22 '13 at 16:41
Sure, you just have to wait for those interpreters to warm up. This works, too: wget -qO- http://www.timeapi.org/utc/now?\\s.\\N –  Camilo Martin Jun 23 '14 at 13:26

Just throwing this out there, but I think the correct formula with the division would be:

echo $(($(date +%s%N)/1000000))
share|improve this answer

My solution is not the best but worked for me.

date +%s000

I just needed to convert a date like 2012-05-05 to milliseconds.

share|improve this answer

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


(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.

share|improve this answer
or you could just use time <script> –  warren Apr 25 '14 at 13:32

For the people that suggest running external programs to get the milliseconds... at that rate, you might as well do this:

wget -qO- http://www.timeapi.org/utc/now?\\s.\\N

Point being: before picking any answer from here, please keep in mind that not all programs will run under one whole second. Measure!

share|improve this answer
You're not asking the local system for the time. Which I guess is implied in the question. You also depend on a network connection. –  orkoden Jan 29 at 11:27
@orkoden The question explicitly asks for "number of milliseconds since Unix epoch January 1 1970". Also, I'm more of pointing out how you shouldn't ever fire up whole of Ruby or Python (or wget) just to get the time - either this is done through a fast channel or milliseconds don't matter. –  Camilo Martin Jan 29 at 11:57
Yes, I understood that you were giving a worse solution to highlight the bad solutions' flaws. I tried several solutions and measured the time. lpaste.net/119499 The results are kind of interesting. Even on a very fast i7 machine date takes 3 ms to run. –  orkoden Jan 29 at 12:38
@orkoden Nice testing! What OS? This might have to do with process spawning overhead. –  Camilo Martin Jan 30 at 13:18
I used OS X 10.10.2. –  orkoden Jan 30 at 16:44

Here is how to get time in milliseconds without performing division. Maybe it's faster...

# test=`date +%s%N`
# testnum=${#test}
# echo ${test:0:$testnum-6}

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.

printf -v test '%(%s%N)T' -1
echo ${test:0:$testnum-6}

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.

share|improve this answer

Using date and expr can get you there i.e.

X=$(expr \`date +%H\` \\* 3600 + \`date +%M\` \\* 60 + \`date +%S\`)
echo $X

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.