Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»YYYY-MM-DD format date in a Shell Script

    YYYY-MM-DD format date in a Shell Script

    RahulBy RahulJuly 26, 20221 Min ReadUpdated:July 27, 2022

    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 %Y-%m-%d or %F options with date command. Here the %F option is an alias for %Y-%m-%d.

      1
      2
      date +%F         #Output: 2022-07-25
      date +%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).

      1
      printf '%(%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')

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to declare boolean variable in shell script
    Next Article Check if a script is running as root user in Linux

    Related Posts

    How to Import GPG Keys on Ubuntu & Debian (without apt-key)

    2 Mins Read

    Running a Cron job every Sunday (Weekly)

    2 Mins Read

    Scheduling a Python Script with Crontab

    2 Mins Read

    Running cron job every 12 hours (twice a day)

    Updated:August 2, 20221 Min Read

    (Resolved) Key is stored in legacy trusted.gpg Keyring

    Updated:August 9, 20222 Mins Read

    How to list all collections in MongoDB database

    1 Min Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Import GPG Keys on Ubuntu & Debian (without apt-key)
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.