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.
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
%Foptions with date command. Here the
%Foption is an alias for %Y-%m-%d.
- Using printf command – Rather than using the date command, the print command also provides a built-in date formatted in bash (>=4.2).
The above methods print the results on standard output. You can also store the results in a variable like: