Shell: How to Get System Current Tme and Format it

time=$(date “+%Y%m%d-%H%M%S”)

or

time=$(date “+%Y-%m-%d %H:%M:%S”)

…… etc. in any format you want

echo “${time}”

The above two lines of simple code is the shell to get the current time and output it in the format you want.

A few things to note

There is a space after date, otherwise the command cannot be recognized, the shell is still very strict about spaces.
Y shows 4-digit year, e.g. 2018; y shows 2-digit year, e.g. 18. m shows month; M shows minute. d shows day, while D shows current date, e.g. 1/18/18 (i.e. 2018.1.18). h shows hour, while h shows month (a bit confusing). s shows current second in milliseconds; S shows current second in seconds.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *