Easy date calculations in Unix scripts with GNU date

When I was writing a post about using date command to confirm date and time in your Unix scripts, I made a note in my future posts list to cover the date calculations – finding out the date of yesterday or tomorrow, and so on. Today I'll show you the simplest way to calculate this.

GNU date command advantage

GNU version of the date command, although supporting a common syntax, has one great option: it allows you to specify the desired date with a simple string before reporting it back. What this means is that by default this specified date is assumed to be "now", but you can use other keywords to shift the result of the date command and thus show what date it was yesterday or a week ago:

Here's a default date output for the current date and time:

ubuntu$ date
Fri Sep 19 08:06:41 CDT 2008

Now, the parameter for specifying desired date is -d or –date, and if you use it with the "now" or "today" parameter, you'll get similar output:

ubuntu$ date -d now
Fri Sep 19 08:06:44 CDT 2008
ubuntu$ date -d today
Fri Sep 19 08:06:50 CDT 2008

Showing tomorrow's date

Similarly, you can get tomorrow's date:

ubuntu$ date -d tomorrow
Sat Sep 20 08:02:12 CDT 2008

Obviously, if you feel like specifying a format for the date, you can do it:

ubuntu$ date -d tomorrow "+%b %d, %Y"
Sep 20, 2008

Find out yesterday's date

Again, there's no rocket science involved in showing yesterday's date neither:

ubuntu$ date -d yesterday "+%b %d, %Y"
Sep 18, 2008

Show a date few days away

If you're interested in a certain date a few days or even weeks away, here's how you can do it:

Example 1: a date 5 days ago

ubuntu$ date -d "-5 days"
Sun Sep 14 08:45:57 CDT 2008

Example 2: a day 2 weeks from now

ubuntu$ date -d "2 weeks"
Fri Oct  3 08:45:08 CDT 2008

Example 3: a day two weeks ago from now

ubuntu$ date -d "-2 weeks"
Fri Sep  5 08:45:11 CDT 2008

Extreme example: a day 50 years ago

If you're really curious about dates in Unix, you can even make GNU date go back a few years:

ubuntu$ date -d "-50 years"
Fri Sep 19 08:47:51 CDT 1958

That's it for today, hope you like this little discovery – having mostly worked with Solaris systems most of my career, I didn't know my Ubuntu had this functionality bonus. Really useful!

See also:

  • yeah, GNU date is very powerful.
    it's actually kind of fun writing loops like:

    echo $date
    date=`date -d "$date +1 day" +%Y-%m-%d`


  • Pingback: Confirm the Day of the Week Based on a Timestamp | UNIX Tutorial: Learn UNIX()

  • danielleb3ar

    how do i get the GMT?

  • Hi Danielle,

    I think it shows you the date according to your current timezone, so if you're in GMT, you'll get all the dates returned in GMT.

  • t.burninator

    If your system isn't set up to display GMT by default (IE; your local time); I believe that you can use "date -u".

    date -u gives "print or set Coordinated Universal Time". Isn't that GMT?


  • Robert

    how do I take a date like 2008/Sep/02 and subract 30 days from it without writing out some sort of awk function? Or is it even possible?

  • DonF

    Late reply for Robert but:

    date -d '30 days ago sep 02 2008'
    Wed Aug 3 20:08:00 PDT 2011

  • DonF

    oops, make that:
    date -d '30 days ago 09/02/2008'
    Sun Aug 3 00:00:00 PDT 2008