About nixCraft

Topics

Getting Yesterdays or Tomorrows day with shell date command

Posted by Vivek Gite [Last updated: December 18, 2007]

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

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:

Discussion on This Article:

  1. kiuze Says:

    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 Says:

    kiuze,

    Thanks for the heads up!

  3. Hebi-kai Says:

    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 Says:

    You can use someting like this to:

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

    or

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

  5. BK Says:

    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 Says:

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

  7. Sri Says:

    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 Says:

    Excellent! Very useful while writing scripts.

    Thanks!!

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!

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

Tags: , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Powered by Open source software.