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.

1. Getting the Current Date and Time

The date command in Bash is a versatile tool for obtaining the current date and time. When used without any options, it provides the current date and time in the default format: “Day_of_week Month Day Hour:Minute:Second Timezone Year”. This comprehensive format includes essential time elements, making it highly informative.

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.


#!/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

2. Formatting Date Output

The date command offers multiple switches for formatting its output. Below are some useful date formats that you can utilize for different purposes.

  1. Get date time in “MM/DD/YY HH:MM:SS” format:
    date +"%D %T" 
    
    03/25/17 14:40:32
    
  2. Get current Unix epoch time:
    date +%s 
    
    1554542637
    
  3. Get date time in “YYYY-MM-DD HH:MM:SS” format:
    date +"%Y-%m-%d %T" 
    
    2019-03-25 14:40:32
    
  4. 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

3. Using Date in Shell Script

In a shell script, the date command is as straightforward to use as it is on the command prompt. To implement this, create a Bash script named ‘getDateTime.sh’ and include the following content within it. This approach allows for easy integration of the date command in automated scripts.


#!/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.

bash 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

4. Available Options with date Command

Discover all the available options for the date command by using the --help parameter, which provides a comprehensive list of functionalities.

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.

Share.

7 Comments

  1. 1) The Bash printf builtin can do all this without command substitution, which creates a new process, which is a very expensive task.

    #!/bin/bash

    printf -v CURRENTDATE ‘%(%F %T)T’ -1
    printf -v CURRENTDATEONLY ‘%(%b %d, %Y)T’ -1

    2) ${EPOCHSECONDS} is a standard Bash shell variable — no need to do anything.

    3) Using “$()” for command substitution is preferred to backticks. It’s easier to read, and it can be nested.

    4) Using ‘${}’ instead of ”$’ is ‘best practice’ to avoid hard to find problems.

  2. 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?

  3. Animesh Kumar on

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

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

      • 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

Leave A Reply


Exit mobile version