Hi All,
I have to kick off a script on every Monday to get some data from database for last week (Sunday thru Saturday).
If its Monday (12/12/2005)
Begin date will be Sunday - 12/4/2005
End date will be Saturday - 12/10/2005
The script might not kick off on some Mondays.
So my question is, if I ran a script today (12/9/2005) how do I find the date of last Saturday (end date) and Sunday (begin date) before last.
I trided to use cal.
Thanks
Look for my datecalc script on this site. You will need it. Then you can do:
$ datecalc -j $(( $(datecalc -j 2005 12 12) - $(($(datecalc -d 2005 12 12)+7-6)) ))
2005 12 10
To explain that...
"datecalc -d 2005 12 12" returns a day-of-week number for the given date. It uses the same scheme as cron..0=sunday 6=saturday. To find out how may days ago the last saturday was we add 7 (days in a week) and subtract 6 (saturday is day 6 in this scheme.
"datecalc -j 2005 12 12" returns a modified julian day number for the given date. 2005-12-12 happens to be 53,716. Each date has a different mjd number. To find the date n days ago, we just subtract n from the mjd. Then we convert the newly computed mjd back to a date.
Be sure to read the "Date Arithmetic" article in our FAQ section. And my datecalc script has built-in help. Just do "datecalc -help".
And the preceding Sunday is always 6 days earlier then the Saturday. Once you have the Saturday you should be able to calculate the Sunday pretty easily.