Languages
[Edit]
EN

Linux / Bash - print current Unix time in milliseconds

1 points
Created by:
Root-ssh
6384

In this short article we would like to show how to print unix time in milliseconds in Unix / Linux terminal.

Quick example:

date +%s%3N

Screenshot: 

millisecond time in Linux / Unix Bash
millisecond time in Linux / Unix Bash

Alternatives

This section shows alternative solutions. 

date +%s%N | cut -b 1-13

# or

echo $(($(date +%s%N) / 1000000))

Note: the first of the solutions above is not perfect as we have extracted 1-13 numbers which will change in the long term due to the increasing number representing time.

Some linux distributions and Unix based operating systems like OS X could do not upport %N, so it can be necessary to use other solution to get it. Below description shows how to do it with different techniques.

Solutions table: 

LanguageConsole command
Rubyruby -e 'puts Time.now.to_f'
Pythonpython -c 'import time; print time.time()'
Node.jsnode -e 'console.log(Date.now())'
PHPphp -r 'echo microtime(TRUE);'
ElixirDateTime.utc_now() |> DateTime.to_unix(:millisecond)
wget + internetuse wget to download time
curl + linkuse curl to download time
date roundeddate +%s000

Example time source for wget and curl:

  1. https://currentmillis.com/
Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.