Getting Yesterdays or Tomorrows day with 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 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?
[a] Ease of use
[b] To write your own scripts
[c] 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
E-mail this to a Friend
Printable Version
You may also be interested in other helpful articles:
- Download of the day: Easy date manipulation with yest utility under Linux/UNIX
- Shell Scripting: Creating report/log file names with date in filename
- Executing script or command on the last day of a month
- Shell script to check / monitor domain renew / expiration date
- Linux / UNIX: display time of different time zones using TZ environment variable
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: cron, current date, date formats, fortnight, gnu, Linux, perl script, scripts, six months, tomorrow tomorrow, UNIX



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
kiuze,
Thanks for the heads up!
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?
You can use someting like this to:
$date -d “yesterday” +%d
$date -d “yesterday” +%m
or
$date -d “yesterday” +%m%d%y
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?
how to find the dates between 2 dates.
i need a command to get the list of dates between 2 dates.
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
Excellent! Very useful while writing scripts.
Thanks!!