Bootstrap FreeKB - Linux Commands - date command
Linux Commands - date command

Updated:   |  Linux Commands articles

The date command with any options or flags will return something like this.

The following is the absoulte minimal code needed to produce the date in a BASH script.

~]# date
Wed Nov 30 19:01:23 CST 2023


Here is how you could add some text before the date in a bash shell script.

echo "The current date and time is $(date)"


Which should produce the following.

The current date and time is Wed Nov 30 19:01:23 CST 2016


The -d or --date option can be used to set a specific date.

~]$ date --date "2023-01-01"
Sun Jan  1 00:00:00 CST 2023


Or to update the date, going forward or backwards x days.

forward_14_days=$(date '+%m-%d-%Y' --date '+14 days')
backwards_14_days=$(date '+%m-%d-%Y' --date '-14 days')


Or months.

forward_six_year=$(date '+%m-%d-%Y' --date '+6 months')
backward_six_year=$(date '+%m-%d-%Y' --date '-6 months')


Or years.

forward_one_year=$(date '+%m-%d-%Y' --date '+1 year')
backward_one_year=$(date '+%m-%d-%Y' --date '-1 year')


Here is an example of how you could format the date.

echo "The current date is $(date '+%m-%d-%Y')"


Which should produce the following.

The current date and time is 11-30-2016


Or like this.

echo "The current date is $(date +%F)"


Following are commonly used format options.

Option Description Example
%Y Year 2018
%y Year 18
%m Month 01
%b Month Jan
%B Month January
%d Day 14
%H Hour (24 hour format) 22
%I Hour (12 hour format) 10
%M Minute 56
%S Second 23
%s Seconds since 01/01/1970 00:00:00 UTC (epoch) 1617673745124
%p AM or PM AM
%P am or pm pm
%a Short day of the week Sun
%A Full day of the week Sunday
%Z Time zone CDT


UTC timezone

The date command will return the date for the timezone of your Linux system. The -u or --utc or --universal flag can be used to return the date in the UTC timezone.

date --utc


Or like this.

utc_time=$(TZ=GMT date)
echo $utc_time


Or like this.

export TZ=GMT
echo $utc_time


Variable and if / else statement

We can put the date in a variable and then use an if / else statement to display certain text based on the statement.

today_epoch=$(date +%s)
some_other_date_epoch=$(date --date "2022-07-03" +%s)

if [ $today_epoch -le $some_other_date_epoch ]; then
  echo True
  echo False


Running this script will produce the following:



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter c8b93e in the box below so that we can be sure you are a human.