Getting Yesterdays or Tomorrows day with shell date command

by Vivek Gite · 19 comments

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 people writing a perl script for calculating yesterday or tomorrow. Computer loves numbers but we love relative terms like 2 days ago. Luckily GNU date command is designed to handle relative date calculation.

Why use relative date formats?

  • Ease of use
  • To write your own 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)

First, print today's date:
$ date
Sun Jun 17 12:17:24 CDT 2007

Now display Yesterday's date:
$ date --date="1 days ago"
OR try:
$ date --date="yesterday"
Sat Jun 16 12:17:20 CDT 2007

Now display Tomorrow's date:
$ date --date="-1 days ago"
Or better try:
$ date --date="next day"
Sat Jun 16 12:17:20 CDT 2007

Getting date in the future

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

Getting date in the past

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

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

To print the date of this Friday:
$ date --date='this Friday'
To print the date of the day six months and 15 day
$ date --date='6 months 15 day'
To print the date of the day two months and 5 days ago:
$ date --date='2 months 5 day ago'

You can also use relative format to setup date and time. For example to set the system clock forward by 30 minutes, enter:
# date --set='+30 minutes'

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

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 19 comments… read them below or add one }

1 kiuze 07.04.07 at 9:19 pm

Thank you for this info!

There seems to be a small typo on first example:

date –date=”1 days ago”

should be

date -–date=”1 days ago”

with two “-”s

2 vivek 07.04.07 at 9:55 pm

kiuze,

Thanks for the heads up!

3 Hebi-kai 07.06.07 at 5:48 pm

How do you use the –date= string to start at one point and count ahead? I want to start at 20060108 and display the date of each sunday until July 1, 2007. If I say “–date=’week’” it gives me one week from today. How can I add ‘week’ to a date, such as 20060108?

4 Pablo Armando 09.18.07 at 6:34 pm

You can use someting like this to:

$date -d “yesterday” +%d
$date -d “yesterday” +%m

or

$date -d “yesterday” +%m%d%y

5 BK 10.25.07 at 12:01 am

Given a date, how do I relatively find a day of the week? Example: For the date 20070807, how do I find out the date of the Monday of that week?

6 sai 10.30.07 at 1:22 pm

how to find the dates between 2 dates.
i need a command to get the list of dates between 2 dates.

7 Sri 11.30.07 at 6:56 am

Hi,
I want previous date and Next date.
I tried the above commands,byt its giving me the following usage.
bash-3.00$ date –date=”yesterday”
date: illegal option — -
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]

I am using HP-UX machine

8 Deiva 06.11.08 at 9:31 am

Excellent! Very useful while writing scripts.

Thanks!!

9 Omri A 12.23.08 at 9:45 am

Or you can use the following to get tomorrow’s date:

x="`date '+%m'`"
y="`date '+%Y'`"
z="`date '+%d'`"

z="`expr ${z} + 1`"
cal ${x} ${y} | grep -w ${z} > /dev/null || {
        z="01"
        x="`expr ${x} + 1`"
        [ "${x}" -lt "10" ] && x="0${x}"
        [ "${x}" = "13" ] &+ 1`"
        }
}

[ "${1}" = "MMDDYYYY" -o "${1}" = "mmddyyyy" -o -z "${1}" ] &${z}${y}"
        echo ${tomorrow}
        exit 0
}
[ "${1}" = "DDMMYYYY" -o "${1}" = "ddmmyyyy" ] &${x}${y}"
} || {
        echo "Invalid format! Using the default:"
        tomorrow="${x}${z}${y}"
}
echo ${tomorrow}
10 Beniori 02.10.09 at 3:15 am

It is very useful. However, I would like to know how can I type the end of month.

11 Andrew McGlashan 03.15.09 at 4:46 pm

I’ve been wanting to work out last day of a month too. I just worked it out:

date -d ‘next month – ‘`date +%d`’day’

Cheers
AndrewM

12 Sourav 03.16.09 at 11:20 am

I am not sure, but when I am trying to use the above command:
date –date=”1 days ago”
I get the following error:
date: illegal option — -
date: illegal option — d
date: invalid argument — te=1 days ago
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
Just FYI: The flavour Unix, I am using is SunOS, and my shell is KORN Shell.

13 Jason Tepoorten 03.19.09 at 12:27 am

Hi Sourav.

I don’t think the Solaris “date” command is as extensive as the LINUX date command.

I this informaiton from Solaris 10’s date command when using “date –date”:
$ date –date
date: illegal option — date
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
$

I found an article on getting yesterday’s date on Solaris from the shell. The article provides a PERL shell command:
perl -e ‘print scalar(localtime(time – 86400)), “\n”‘

The URL for that article is http://sysunconfig.net/unixtips/prevdate.txt

14 Jason Tepoorten 03.19.09 at 1:10 am

Hi Sourav.

I’ve done some digging for determining yesterday’s and tomorrow’s date and I found a Korn Shell script posted at the URL http://www.computing.net/answers/unix/getting-old-date-into-variable/5223.html. Look for Response 10 for the source of the KSH script.

This KSH script runs under KSH on Solaris 10.

15 Sourav 03.24.09 at 10:49 am

Hi Jason,

Firstly thanks a ton to find out the source of my problem.

But just FYI, i tried to give the below commands, and it helped me to get yesterday’s and tomorrow’s date. Hope this helps everyone who faces the same issue as me.

$ TZ=$TZ+24 date “+%D”……….for yesterday’s date
$ TZ=$TZ-24 date “+%D”………..for tomorrow’s date

Thanks again…

16 Ibrahim 07.12.09 at 2:55 pm

The “date –date” option only works with GNU version of date. It will most probably not work on AIX, Solaris or HP-UX. If you want to calculate yesterday’s date, you can also use this shell script.

Slight modification may be necessary to calculate tomorrow’s date.

17 'Tosin 09.25.09 at 3:28 pm

Thanks so much, this has been so helpful.

18 Aleks 11.18.09 at 5:15 pm

@Andrew McGlashan
thx a whole bunch!

19 Ibrahim Khaleel 12.02.09 at 2:47 pm

I wanted output as this format – 2009_12_01, the below worked for yesterday
echo `date –date=”yesterday” ‘+20%y_%m_%d’`

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: