Saturday, June 18, 2022

Linux Epoch time and obtain Unix/Linux Epoch Time in Milliseconds

Command to get Epoch time: $ date +%s

This will print the current epoch time in seconds since January 1, 1970.

---

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

is used to obtain the current time in milliseconds since the Unix epoch (January 1, 1970, UTC). Here's a breakdown of how this command works:

date +%s%N: This part of the command uses the date command with the format specifier %s%N to obtain the current time in seconds since the Unix epoch followed by the current nanoseconds. %s gives the seconds, and %N gives the nanoseconds.

$(...): The $(...) syntax is used to capture the output of the date command and evaluate it as an arithmetic expression.

/1000000: This part divides the result obtained from date +%s%N by 1,000,000 to convert nanoseconds to milliseconds. Since there are 1,000,000 nanoseconds in a millisecond, this division gives you the time in milliseconds.

***

No comments:

Post a Comment