Bash: How to Get Time in Milliseconds (With Example)


You can use the following syntax in Bash to get the current time displayed with milliseconds:

date +"%T.%3N"

Note that the date command returns the current date and time and the + operator specifies how the current date and time should be formatted.

By using %T we specify that we only want to display the time portion of the datetime and by using %3N we specify that we want to only show the first 3 digits for the number of nanoseconds, which is actually equivalent to the number of milliseconds.

We could also use the following syntax to display the date along with the time in milliseconds:

date +"%Y-%m-%d %H:%M:%S.%3N"

The following example shows how to use each of these methods in practice.

Example: How to Get Time in Milliseconds in Bash

We can use the following syntax to return the current time (with milliseconds) in Bash:

date +"%T.%3N"

The following screenshot shows how to use this syntax in practice:

Bash get time with milliseconds

Notice that this returns the current time with milliseconds:

  • 09:54:21.779

Note that this represents 9:54 AM with 21 seconds and 779 milliseconds.

If we’d like, we could also use the following syntax to return the date along with the time in milliseconds:

date +"%Y-%m-%d %H:%M:%S.%3N"

The following screenshot shows how to use this syntax in practice:

Bash get datetime with milliseconds

We can see that this returns the current date and time with milliseconds in the following format:

  • 2024-07-26 09:54:49.475

Feel free to use the + operator to format the date and time in any way that you would like.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

How to Compare Dates in Bash
How to Convert Timestamp to Date in Bash
How to Create a Timestamp Variable in Bash

Leave a Reply