Mohammedz.com

For Linux and Shell scripting.


Leave a comment

How to find past/future dates?

what day it was exactly one month ago? Are you counting down the days to find it? Or searching for a calender?

You can use “date” command in such situations. See the example below:

****************************************
-bash-3.1$ date +%F
2009-06-29
-bash-3.1$ date -d”1 day ago” +%F
2009-06-28
-bash-3.1$ date -d”1 week ago” +%F
2009-06-22
-bash-3.1$ date -d”1 month ago” +%F
2009-05-29
-bash-3.1$ date -d”1 year ago” +%F
2008-06-29
-bash-3.1$
****************************************

These examples will show you how to find the future dates.
****************************************
-bash-3.1$
-bash-3.1$ date +%F
2009-06-29
-bash-3.1$ date -d”-1 day ago” +%F
2009-06-30
-bash-3.1$ date -d”-1 week ago” +%F
2009-07-06
-bash-3.1$ date -d”-1 month ago” +%F
2009-07-29
-bash-3.1$ date -d”-1 year ago” +%F
2010-06-29
-bash-3.1$
****************************************

~mohammed