Date

From Hack Sphere Labs Wiki
Revision as of 17:07, 1 November 2013 by Webdawg (talk | contribs) (Created page with "*http://blog.sleeplessbeastie.eu/2011/12/15/how-to-find-yesterdays-or-tomorrows-date-using-shell/ How to find yesterday's or tomorrow's date using shell? Posted on December,...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


How to find yesterday's or tomorrow's date using shell? Posted on December, 15 2011 by milosz

There are couple of different ways to perform this task using shell date command depending on used OS. I will concentrate on Ubuntu Linux, OpenBSD and FreeBSD. Ubuntu Linux

Ubuntu Linux offers -d switch to find the answer.

$ date --version
date (GNU coreutils) 8.5
...
Current date:

$ date
Thu Dec 15 21:37:53 CET 2011

Calculate yesterday's date:

$ date -d "yesterday"
Wed Dec 14 21:38:02 CET 2011
$ date -d "last day"
Wed Dec 14 21:38:03 CET 2011
$ date -d "-1 day"
Wed Dec 14 21:38:05 CET 2011

Calculate tomorrow's date:

$ date -d "tomorrow"
Fri Dec 16 21:39:20 CET 2011
$ date -d "next day"
Fri Dec 16 21:39:22 CET 2011
$ date -d "+1 day"
Fri Dec 16 21:39:25 CET 2011

OpenBSD

Using OpenBSD this task needs to be performed in different way as there is no equivalent switch.

Current date:

$ date
Thu Dec 15 22:15:07 CET 2011

Calculate yesterday's date:

$ date -r `expr $(date +%s) - 86400` 
Wed Dec 14 22:15:09 CET 2011

Calculate tomorrow's date:

$ date -r `expr $(date +%s) + 86400` 
Fri Dec 16 22:15:11 CET 2011

FreeBSD

FreeBSD offers handy -v switch to perform this task.

Current date:

$ date
Thu Dec 15 22:18:14 CET 2011

Calculate yesterday's date:

$ date -v-1d
Wed Dec 14 22:18:18 CET 2011

Calculate tomorrow's date:

$ date -v+1d
Fri Dec 16 22:18:22 CET 2011