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:
- man date
- GNU/date documentation
Updated for accuracy!
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop














{ 48 comments… read them below or add one }
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
Is there any way to caculate the current staturday’s date based on the input date passed not from the system date?
Please let me know
Thanks,
Rajesh
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
Thank you..it is working :)
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
I am having similar error on HP. How did you resovle your problem?
I do not think HP-UX uses the GNU tools, such as /bin/date.
You need more that just bash, presuming you typed “date –date=….”
In other words, “date {dash{dash} date=”
There are many little details like that which make HP-UX annoying.
I am spoiled by Linux.
Excellent! Very useful while writing scripts.
Thanks!!
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}It is very useful. However, I would like to know how can I type the end of month.
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
@Andrew & @Sourav
May be wordpress word entry has eaten some of your characters,
here is an equivalent rephrase of what you gave,
to print last day of current month on GNU/BASH shell
date --date "next month - $(date +%d) day"
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.
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
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.
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…
Hi Sourav.
I apologise for the late reply. I never got a posting notification :(.
I’ll try this command on my client’s UltraSPARC IV SunOS 10.0 KSH environment.
How did you find this one?
Kind Regards,
Jason TEPOORTEN
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.
Thanks so much, this has been so helpful.
@Andrew McGlashan
thx a whole bunch!
I wanted output as this format – 2009_12_01, the below worked for yesterday
echo `date –date=”yesterday” ‘+20%y_%m_%d’`
How to find the commands which are executed yesterday?
@Kassim
Use the command
$ history
How this this done in AIX?
I usually find really helpful articles in this site….
Keep up the good work:)
doesn’t work on Mac OS X
Hello guys,
Here is a nice one it calculate the latest working day of the month maybe it’s usefull
TODAY=`date +%d-%m-%Y`
if [ ${TODAYR} -eq 5 ]
then
DD2=`TZ=”GMT-72″ date +’%d’`
else
DD2=`TZ=”GMT-24″ date +’%d’`
fi
if [ ${DD} -gt ${DD2} ]
then
another script on the latest work day of the month
else
exit
Hello guys,
(i missed the TODAYR (is day number from 1-7) in my prev email)
TODAY=`date +%d-%m-%Y`
TODAYR=`date +%u`
if [ ${TODAYR} -eq 5 ]
then
DD2=`TZ=”GMT-72″ date +’%d’`
else
DD2=`TZ=”GMT-24″ date +’%d’`
fi
if [ ${DD} -gt ${DD2} ]
then
It was very helpfully for me.
Thanks
You have a little flaw in there:
doesn’t return the date two months and five days ago but rather the date which was five days before the date which will be in two months. It tells date something like “plus 2 months minus 5 days”.
Of course, this might be what you wanted. But to be corresponding to your descripten, it would have to be
Yes Rigth Nico.
Vivek should amend this one, right Vivek ?
@Nico / Philippe,
This article has been updated with information on the date command with additional examples.
It does work but it should look like:
date –date “-2 months 5 days ago”
Hi Victor.
I am afraid syntax is unclear to you.
Regarding each token (day, month) syntax may be either using minus sign, either using “ago” word. So that all 4 expressions are referring to one unique date:
date –date “2 months ago 5 days ago”
date –date “- 2 months – 5 days”
date –date “2 months ago – 5 days”
date –date “- 2 months 5 days ago”
Is it clear to you now?
– Philippe
hello –
I am looking for some ideas to print out dates in between 2 dates in YYYY-mm-dd format in unix.
any advice is appreciated.
thanks
for ((i=”2010-12-01″; i<="2011-06-14"; $currentDateTS+=86400))
do
echo $i"\n"
done
i know something is not right here please advice.
we are using this command in our pull script to get day-1 data.
vYdy_yyyy_mm_dd=`date –d=’1 days ago’ +”%Y-%m-%d”`
But i want data from 2010-08-1 to 2010-05-19 at a time.Could you please help me out in changing the command.
vYdy_yyyy_mm_dd=`date –d=’1 days ago’ +”%Y-%m-%d”`
Hi all,
I am having a script to check filesize’s difference between two consecutive days dumps. The DB dumps are created every WORKING day (Monday-Friday) and they all have the same name + the full name day of the week, i.e. dumpFile.Monday.dump, dumpFile.Tuesday.dump … dumpFile.Friday.dump.
In my script I try to use `date +%A` for today and `date –date=yesterday +%A` for yesterday, however this (obviously) doesn’t work for Monday, when I get yesterday as being Sunday, whereas I want Friday.
I hacked a bit the script with an if loop, if today = Monday, then use yesterday = Friday, else use `date –date=yesterday +%A`, but is not quite an elegant solution.
I wonder if there’s a way that on any given date, to be able to get previous WORKING day.
Cheers
Hi Chunky,
You post is old, but here is an answer.
Any system can only elaborate data based on what you feed it with, data & algorithm.
A “working day” is a humain professional concern, and, AFAICS, this information is not stored _yet_ in any standard *nix system.
hacked a bit the script with an if loop, if today = Monday, then use yesterday = Friday, else use `date –date=yesterday +%A`, but is not quite an elegant solution.
Elegant or not, system has to store this information before giving it back to you.
FTTOMH, as you want to compare the 2 most recent dump files, another way to do it without this worrying about working days would simply be to select the 2 most recent dump files to compare them, based on modification time.
Nevertheless, on one hand, this involves at least extra coding instead of a simple test, and may be less secure, for instance if you do not check that every dump went fine.
On the other hand, this algorithm would work in any situation, that is, it would always test the 2 most recent dump files, whatever calendar events, closing days, holidays, whatsoever.
What do you think Chunky?
– Philippe
Hey I make a query..please let me know if this is possible…
StartDate=`date –date=’1 day ago’ +”%y%m%d”`
i am using above command to use yesterday’s date. In “1 day ago” i want to make 1 as variable like $i to change it when needed. as 1,2,3 etc…
Hey Chunky,
here’s another take on your problem if you can afford to have an extra binary that does the job. Look at my dateutils project (http://hroptatyr.github.com/dateutils). It can deal with business days natively, use the b suffix:
dadd 2012-04-16 -1b
=> 2012-04-13
date –date “3 month – $(date +%d) day + 1 day”
tried it using Philippe Petrinko and adding one day, this show the first day of the mont in 3 month. Just to show how much you can do.
date -d ‘-3 days -2 hours -10 minutes’ ‘+%d/%m/%Y %H:%M’ , this second part changes the format.
Thanks for this tutorial
I found that when the clocks are set back an hour in November, here in the US, I could not use 1 day ago in the date command and have to use 26 hour ago. If you don’t do that you could accidentally work with the wrong file.
Just beautiful…