Mutual conversion of shell nanosecond/millisecond timestamp

Get timestamp (nanoseconds)
echo $[$(date +%s%N)]


get timestamp (milliseconds)
echo $[$(date +%s%N)/1000000]
get timestamp (milliseconds) 
echo $[$(date +%s%N)/1000000]
or
date +%s%3N
specific Convert time to timestamp (nanoseconds)
date -d "2015-08-04 00:00:00.1232133" +%s%N


Convert specific time to timestamp (seconds)
date -d "2015-08-04 00:00:00" +%s


From timestamp to time and
 nanoseconds to date
date -d @1593775634.076773670 +"%F %H:%M:%S.%N"
The parameter unit of date is second, remember to convert the number.

Add N minutes to the specified time
date -d "20150416 12:20:10 10 minute ago" + "%Y%m%d%H%M%S"
# 20150416121010
 
date -d "20150416 12:20:10 10 minute" +"%Y%m%d%H%M%S"
# 20150416123010

For example: the timestamp of 1639647312872 is used to obtain the system time, because linux does not have the concept of millisecond timestamp, so it needs to be manually converted to milliseconds.

date -d @1639647312.872

result

Thu Dec 16 17:35:12 CST 2021

Guess you like

Origin blog.csdn.net/leonnew/article/details/123638146