The Linux date command is used to get the current date and time. You can easily customize the results by using the arguments. In this how-to tutorial, you will learn to format the date as “YYYY-MM-DD” in the bash shell.
Advertisement
Use one of the below methods to get the current date in “YYYY-MM-DD” format.
- Using date command – To print the results in YYYY-MM-DD format, you can use
%Y-%m-%d
or%F
options with date command. Here the%F
option is an alias for %Y-%m-%d.12date +%F #Output: 2022-07-25date +%Y-%m-%d #Output: 2022-07-25 - Using printf command – Rather than using the date command, the print command also provides a built-in date formatted in bash (>=4.2).1printf '%(%Y-%m-%d)T\n'
The above methods print the results on standard output. You can also store the results in a variable like:
1 2 3 4 5 | dt=$(date '+%Y-%m-%d') dt=$(date '+%F') dt=$(printf '%(%Y-%m-%d)T\n') |