Bash: Some Date Functions

Some of the date functions I have had to use recently in bash shell

Get Current Date

today_date=$(date '+%Y%m%d')

(in format YYYYMMDD)

Get Yesterday

yesterday_date=$(date -d yesterday '+%Y%m%d')

(in format YYYYMMDD)

Get Day of Week

dayOfWeekNumber=$(date +%u)


dayOfWeekName=$(date +%A)


Could pass in a specific date:

dayOfWeekNumber=$(date -d”20190821″ +%u)

or a variable:

dayOfWeekName=$(date -d”{dateToCheck}” +%A)

Subtract Days from Date

E.g. Take 1 day from 21/08/2019

new_date=$(date -d"20190821 -1 day" +%Y%m%d)

Of course, the date and the number of days could be variables:

new_date=$(date -d”${old_date} -${num_days} day” +%Y%m%d)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s