Getting Yesterdays or Tomorrows Day With Bash Shell Date Command

When invoked without arguments, the date command displays the current date and time. Depending on the options specified, date will set the date and time or print it in a user defined way. I’ve seen many sysadmin writing perl scripts for calculating relative date such as yesterdays or tomorrows day. You can use GNU date command, which is designed to handle relative date calculation such as:

  • 1 Year
  • 2 Days
  • 2 Days ago
  • 5 Years

GNU date syntax

The syntax is as follows:

 
date  --date="STRING"
date  --date="next Friday"
date  --date="2 days ago"

The –date=STRING is a human readable format such as “next Thursday” or “1 month ago”. A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers.

Why Use Relative GNU/date Formats?

  • Ease of use
  • Write your own shell scripts
  • Automate task using cron (example run a job on last day of the month or Nth day of the month or 3rd Friday and so on)

Examples

First, to display today’s date, enter:
$ date
Sample outputs:

Wed Jun 15 04:47:45 IST 2011

To display yesterday’s date, enter:
$ date --date="1 days ago"
OR
$ date --date="1 day ago"
OR
$ date --date="yesterday"
OR
$ date --date="-1 day"
Sample outputs:

Tue Jun 14 04:54:40 IST 2011

You can use various string formats to produce the same output. Please note that the output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like IST.

Find tomorrow’s date

Type the following command
$ date --date="-1 days ago"
Or
$ date --date="next day"

Getting Date In the Future

To get tomorrow and day after tomorrow (tomorrow+N) use day word to get date in the future as follows:

 
date --date='tomorrow'
date --date='1 day'
date --date='10 day'
date --date='10 week'
date --date='10 month'
date --date='10 year'

The date string ‘tomorrow’ is worth one day in the future which is equivalent to ‘day’ string i.e. first two commands are same.

Getting Date In the Past

To get yesterday and earlier day in the past use string day ago:

 
date --date='yesterday'
date --date='1 day ago'
date --date='10 day ago'
date --date='10 week ago'
date --date='10 month ago'
date --date='10 year ago'

The date string ‘yesterday’ is worth one day in the past which is equivalent to ‘day ago’ string i.e. first two commands are same.

Moving By Whole Years or Months

You can add year and months keywords to get more accurate date:
$ date --date='2 year ago' # Past
$ date --date='3 years' # Go into future
$ date --date='2 days' # Future
$ date --date='1 month ago' # Past
$ date --date='2 months' # Future

Moving Date Using More Precise Units

  • You can use fortnight for 14 day.
  • week for 7 days.
  • hour for 60 minutes
  • minute for 60 seconds
  • second for one second
  • You can also use this / now / today keywords to stress the meaning.

Few examples using precise string units:

 
date --date='fortnight'
date --date='5 fortnight'
date --date='fortnight ago'
date --date='5 fortnight ago'
date --date='2 hour'
date --date='2 hour ago'
date --date='20 minute'
date --date='20 minute ago'

Moving Date Using the Day of Week Items

To print the date of this Friday, enter:

 
date --date='this Friday'
## OR ##
date --date='next Friday'

Days of the week may be spelled out in full: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday or Saturday. Days may be abbreviated to their first three letters, optionally followed by a period

 
date --date='this Fri'
## OR ##
date --date='next Fri.'

You can also move forward supplementary weeks as follow:

 
date --date='2 Fri'
## OR ##
date --date='second Fri.'
## OR ##
date --date='Second Friday'
####
####  last DAY or next DAY move one week before or after the day that DAY by itself
####
date --date='last Friday'
date --date='next Friday'

To print the date in the future ($now + 6 months + 15 days), enter:
$ date --date='6 months 15 day'
To print the date in the past [$now – (two months and 5 days ago) ], enter:
$ date --date='-2 months 5 day ago'

Display Date Using Epoch Time

To display date in epoch time:
$ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s

How Do I Use Relative Date Format To Set System Date & Time?

You can also use relative format to setup date and time. For example to set the system clock forward by 30 minutes, enter (you must be login as root to set the system date and time):
# date --set='+30 minutes'
OR
# date --set='1 day ago'
OR
# date --set='5 day'
However, I recommend setting NTPD client / server or OpenNTPD server to synchronize the local clock of a computer system with remote NTP servers.

How Do I Assigned Yesterday To Shell Variable?

Use the following syntax (see assign values to shell variables and command substitution for more information)

 
yest=$(date --date="yesterday")
echo "$yest"
yest=$(date --date="yesterday" +"%d/%m/%Y")
echo "The backup was last verified on $yest"

Recommend readings:

  1. man date
  2. GNU/date documentation

Updated for accuracy!

Advertisements