Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Get Current Date and Time in Bash Script

    How to Get Current Date and Time in Bash Script

    By RahulFebruary 28, 20234 Mins Read

    As a scripting language, Bash is widely used for system administration tasks and automation. One common task in Bash scripting is to get the current date and time. The `date` command returns the current date and time in a specific format, but you can also use a custom format string to get the date and time in any format

    Advertisement

    In this article, we will explore how to get the current date and time in a Bash script.

    Getting the Current Date and Time

    In Bash, you can use the date command to get the current date and time. By default, the date command will return the current date and time in the format of “Day_of_week Month Day Hour:Minute:Second Timezone Year”

    For example:

    date 
    
    Sun Feb 28 23:26:13 UTC 2023
    

    You can also store the output of command in a variable for further use.

    1
    2
    3
    4
    5
    #!/bin/bash
    current_date_time=$(date)
    echo "Current date and time: $current_date_time"
     
    # Output: Current date and time: Sun Feb 28 23:26:13 UTC 2023

    Custom Format Output

    There are several switches, you can use to format the output of date command.

    • Get date time in “MM/DD/YY HH:MM:SS” format:
      date +"%D %T" 
      
      03/25/17 14:40:32
      
    • Get current Unix epoch time:
      date +%s 
      
      1554542637
      
    • Get date time in “YYYY-MM-DD HH:MM:SS” format:
      date +"%Y-%m-%d %T" 
      
      2019-03-25 14:40:32
      
    • Here is more common date time formats:
      ParameterOutput
      date +”%m/%d/%Y”03/25/2019
      date +”%d-%b-%Y”25-Mar-2019
      date +”%Y %b %m”2019 Mar 25
      date +”%H:%M”14:40
      date +”%I:%M %p”02:40 PM
      date +”%H:%M:%S”14:40:32
      date +”%I:%M:%S %p”02:40:32 PM
      date +”%m/%d/%Y %H:%M”03/25/2019 14:40
      date +”%A, %m %d %Y %H:%M”Monday, 03 25 2019 14:40
      date +”%A, %b %d, %Y %I:%M %p”Monday, Mar 25, 2019 02:40 PM
      date +”%A, %b %d, %Y %H:%M:%S”Monday, Mar 25, 2019 14:40:32

    Use Date in Shell Script:

    You can simply use date command inside shell script similar to use on the command prompt. Create a bash script getDateTime.sh with the following content.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!/bin/bash
     
    CURRENTDATE=`date +"%Y-%m-%d %T"`
    CURRENTDATEONLY=`date +"%b %d, %Y"`
    CURRENTEPOCTIME=`date +"%Y-%m-%d %T"`
     
    echo Current Date is: ${CURRENTDATEONLY}
    echo Current Date and Time is: `date +"%Y-%m-%d %T"`
    echo Current Date and Time is: ${CURRENTDATE}
    echo Current Unix epoch time is: ${CURRENTEPOCTIME}

    Now execute the script from command line and watch output.

    ./getDateTime.sh 
    
    Current Date is: Mar 25, 2019
    Current Date and Time is: 2019-03-25 17:18:19
    Current Date and Time is: 2019-03-05 17:18:19
    Current Unix epoch time is: 1488541699
    

    Available Options with date Command:

    You can find all available options of date command using –help parameter

    date --help 
    

    You will find the output like below with some more options.

    • %%: a literal %
    • %a: locale’s abbreviated weekday name (e.g., Sun)
    • %A: locale’s full weekday name (e.g., Sunday)
    • %b: locale’s abbreviated month name (e.g., Jan)
    • %B: locale’s full month name (e.g., January)
    • %c: locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
    • %C: century; like %Y, except omit last two digits (e.g., 21)
    • %d: day of month (e.g, 01)
    • %D: date; same as %m/%d/%y
    • %e: day of month, space padded; same as %_d
    • %F: full date; same as %Y-%m-%d
    • %g: last two digits of year of ISO week number (see %G)
    • %G: year of ISO week number (see %V); normally useful only with %V
    • %h: same as %b
    • %H: hour (00..23)
    • %I: hour (01..12)
    • %j: day of year (001..366)
    • %k: hour ( 0..23)
    • %l: hour ( 1..12)
    • %m: month (01..12)
    • %M: minute (00..59)
    • %n: a newline
    • %N: nanoseconds (000000000..999999999)
    • %p: locale’s equivalent of either AM or PM; blank if not known
    • %P: like %p, but lower case
    • %r: locale’s 12-hour clock time (e.g., 11:11:04 PM)
    • %R: 24-hour hour and minute; same as %H:%M
    • %s: seconds since 1970-01-01 00:00:00 UTC
    • %S: second (00..60)
    • %t: a tab
    • %T: time; same as %H:%M:%S
    • %u: day of week (1..7); 1 is Monday
    • %U: week number of year, with Sunday as first day of week (00..53)
    • %V: ISO week number, with Monday as first day of week (01..53)
    • %w: day of week (0..6); 0 is Sunday
    • %W: week number of year, with Monday as first day of week (00..53)
    • %x: locale’s date representation (e.g., 12/31/99)
    • %X: locale’s time representation (e.g., 23:13:48)
    • %y: last two digits of year (00..99)
    • %Y: year
    • %z: +hhmm numeric timezone (e.g., -0400)
    • %:z: +hh:mm numeric timezone (e.g., -04:00)
    • %::z: +hh:mm:ss numeric time zone (e.g., -04:00:00)
    • %:::z: numeric time zone with : to necessary precision (e.g., -04, +05:30)
    • %Z: alphabetic time zone abbreviation (e.g., EDT)

    Conclusion

    Getting the current date and time in a Bash script is a common task that can be accomplished using the date command. By default, the date command returns the current date and time in a specific format, but you can also use a custom format string to get the date and time in any format you like. By understanding how to use the date command in a Bash script, you can build powerful automation scripts that make use of the current date and time.

    date date command linux date
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Getting Yesterday’s Date in Bash: A Practical Walkthrough

    Getting Yesterday’s Date in Bash: A Practical Walkthrough

    Getting Tomorrow’s Date in Bash: A Practical Walkthrough

    Getting Tomorrow’s Date in Bash: A Practical Walkthrough

    JavaScript Techniques for Determining Past Dates

    View 6 Comments

    6 Comments

    1. William Old on December 30, 2021 11:20 pm

      Line 5 uses the same formatting string as line 3… shouldn’t the string be %s?

      Reply
    2. vishal on May 5, 2021 12:55 pm

      what ever is date and time today, how can I get start of today where hh:mm:ss is “00:00:00” for UTC timezone?

      Reply
    3. Zak on August 13, 2020 4:26 pm

      Thankfulness to my father who stated to me about this blog, this webpage is
      actually remarkable.

      Reply
    4. Animesh Kumar on June 6, 2019 4:41 am

      CURRENTEPOCTIME=`date +Y-%m-%d %T”` this is wrong.

      CURRENTEPOCTIME=`date +”%Y-%m-%d %T”` this is correct.

      Reply
      • Rahul on June 6, 2019 4:47 am

        Thanks Animesh, Corrected the tutorial.

        Reply
        • ananya on August 5, 2021 6:55 am

          Hi I need a script to write in pipeline to extend vm where it need to ask for extend life that supports the below format:
          NNN-Extend for number of days
          DD/MM/YYYY- Date
          N m-Extend for n months
          N y- Extend for n years

          Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Python Function with Parameters, Return and Data Types
    • free Command in Linux (Check Memory Uses)
    • Git Rebase: A Practical Guide
    • How to move the complete Git repository
    • Handling Special Characters in Shell Scripts
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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