You may simply use %3N to truncate the nanoseconds to the 3 most significant digits (which then are milliseconds):
$ date +%s%3N
1397392146866
This works e.g. on my Kubuntu 12.04 (Precise Pangolin).
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
1397392146%3N
(And also my (non rooted) Android tablet doesn't have %N.)
Update: Another alternative in pure Bash that works only with Bash 4.2+ is the 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
testnum=${#test}
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.
I have noticed that the MacOS' version of the date command is not interpreting the %N format sequence as nanoseconds but simply prints N to the output when I started using my .bashrc script from Linux, that's using it to measure how long executed commands run, on a MacOS machine.
After a little bit of research, I have learned that only the GNU date from the GNU Coreutils package does support milliseconds. Fortunately, it's pretty easy to install it on MacOS using Homebrew:
brew install coreutils
Since that package contains executables that are already present on MacOS, Coreutils' executables will be installed with a g prefix, so date will be available as gdate.
This:
will return the number of seconds since the epoch.
This:
returns the seconds and current nanoseconds.
So:
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 (Precise Pangolin).
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 doesn't have
%N
.)date +%N
doesn't work on OS X, but you could use one ofruby -e 'puts Time.now.to_f'
python -c 'import time; print(int(time.time() * 1000))'
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
My solution is not the best, but it worked for me:
I just needed to convert a date like 2012-05-05 to milliseconds.
Just throwing this out there, but I think the correct formula with the division would be:
This solution works on macOS.
If you consider using a Bash script and have Python available, you could use this code:
For 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!
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 coercesbc
to do what you want. There are lots more!I only tested this on Windows 7/MinGW... I don't have a proper *nix box at 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 the 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. Seeman strftime
for supported options. Also seeman bash
to seeprintf
syntax.-1
and-2
are special values for time.Another solution for MacOS: GNU Coreutils
I have noticed that the MacOS' version of the
date
command is not interpreting the%N
format sequence as nanoseconds but simply printsN
to the output when I started using my.bashrc
script from Linux, that's using it to measure how long executed commands run, on a MacOS machine.After a little bit of research, I have learned that only the GNU date from the GNU Coreutils package does support milliseconds. Fortunately, it's pretty easy to install it on MacOS using Homebrew:
Since that package contains executables that are already present on MacOS, Coreutils' executables will be installed with a
g
prefix, sodate
will be available asgdate
.See for example this page for further details.