Bash Script Dates and Times

When working with dates and times in Bash Script, the built in 'date' command can be used.

echo `date`

Note that, in order for 'date' to be seen as a command, it must be preceded and followed by a back tick, often located to the left of the number one key on the keyboard.

The output of the 'date' command will vary slightly depending on system settings, but an example can be seen below.

Sat Sep 9 12:32:19 PM BST 2023

The 'date' command can also be used to assign the date and time to a variable and display it from there.

TODAY=`date`
echo $TODAY

This will produce the same output as before.

Often, when dates and times are required, this isn't an ideal format. Bash Script allows for a number of different formats to be used.

DATE=`date +%A,\ %d\ %B\ %Y`
echo Date: $DATE
TIME=`date +%H:%M`
echo Time: $TIME

This will display the date and time as follows. It should be noted that in order to get a space in the date format, it must be preceded by a '\' symbol. This acts as an escape character because Bash Script doesn't allow spaces in the format pattern without it.

Date: Saturday, 09 September 2023
Time: 15:29

Below is a table showing format patterns that can be used when displaying dates and times.

Format
Pattern
Description
date +%a Three character abbreviation for the day of the week e.g., Mon for Monday.
date +%A Full name for the day of the week e.g., Monday.
date +%b Three character abbreviation for the month e.g., Mar for March.
date +%B Full name for the month e.g., March.
date +%d Day of the month as a number from 01 through 31.
date +%D Current date, MM/DD/YYYY, month, day, and year format.
date +%F Current date, YYYY-MM-DD, year, month, and day format.
date +%H Hours, using a 24 hour clock, with a leading zero e.g., 20.
date +%I Hours, using a 12 hour clock, with a leading zero e.g., 05.
date +%j Day of the year, 001 through 366.
date +%m Month as a number, with a leading zero e.g., 03 for March.
date +%M Minutes, with a leading zero e.g., 06.
date +%S Seconds, with a leading zero e.g., 08.
date +%T Time, HH:MM:SS, 24 hour clock, in hours, minutes, and seconds format.
date +%u Day of the week as a number e.g., 1 for Monday, 2 for Tuesday, and so on.
date +%U Week of the year as a number, 00 through 53.
date +%y Year, without the century but with a leading zero e.g., 09.
date +%Y Year, including the century e.g., 2023.
date +%Z Current time zone as a three letter abbreviation.

If necessary, it is also possible to manipulate the current date and time by adding or subtracting from each component. Below are examples of how this can be done.

echo Current date/time minus two years: `date -d "2 year ago"`
echo Current date/time minux two months: `date -d "2 month ago"`
echo Current date/time minus two days: `date -d "2 day ago"`
echo Current date/time minus two hours: `date -d "2 hour ago"`
echo Current date/time minus two minutes: `date -d "2 minute ago"`
echo Current date/time minus two seconds: `date -d "2 second ago"`
echo Current date/time add two seconds: `date -d "2 second"`
echo Current date/time add two minutes: `date -d "2 minute"`
echo Current date/time add two hours: `date -d "2 hour"`
echo Current date/time add two days: `date -d "2 day"`
echo Current date/time add two months: `date -d "2 month"`
echo Current date/time add two years: `date -d "2 year"`