Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»YYYY-MM-DD format date in a Shell Script

    YYYY-MM-DD format date in a Shell Script

    By RahulAugust 15, 20221 Min Read

    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.

      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

    Related Posts

    A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)

    Setting Up Permissions in Home Directory on Linux

    Configuring Apache Userdir on Ubuntu and Debian: A Simplified Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)
    • Understanding Basic Git Workflow: Add, Commit, Push
    • The Difference Between Git Reset –soft, –mixed, and –hard
    • Understanding the Staging Area in Git’s Workflow
    • Python Function with Parameters, Return and Data Types
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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