Calculating 7 days ago date for the given Argument

Hi

I have shell script and I am facing the below issue to integrate the date calculation to the the script.

If I give the $1 as the date(20110701) then I need to get the 7 days ago date for the same format.(20110624).

At first I thought its a simple one to handle and I did a search in the forum but couldn't get this if we provide the date as the $1 argument.

For example:

if $1 = 20110601 ( The format I would provide)
then I need to calculate the date for 7 days ago i.e. 20110525.

Here I am not using the system current date but instead I have to calculate the 7 days ago date for the date that the user has given.

Could someone please help me out in this. Its really appreciated.

What is your system? What is your shell? This has a huge effect on the tools you have available for playing with dates. On some systems you can just date -d and print dates and timestamps for whatever time you want. Others might have awk or perl extensions for it. And a few obnoxious ones have no elegant way to handle it and must use an enormous ksh script to parse dates the hard way.

Hi

I am using Linux system -->

Linux xbdp5898pap 2.4.21-50.ELsmp #1 SMP Tue May 8 17:18:29 EDT 2007 i686 i686 i386 GNU/Linux

I am using Bash Shell.

Like if I use date -d then it would take the system date and then we can print wat we want.But Instead I would like to use the User's date(20110701) and then calculate the 7 days ago date (20110624) and print as an output.

Thanks for your thoughts. Really appreciate for your time.

You have GNU date, which supports -d, letting you input whatever date you want which makes this tons easier. Working on something

[edit] You stealth-edited my stealth edit, Jim :smiley:

[edit] whoops, %Y%m%d not %Y%m%s!

# Get epoch seconds
USERDATE=$(date +%s -d 20110701)
# Subtract 1 week
((USERDATE -= (60*60*24*7) ))

# Convert from epoch seconds into YYYYMMDD.
# -d assumes strings beginning with @ are epoch seconds.
D=$(date -d "@$USERDATE" +"%Y%m%d")
echo "One week previous was $D"

This can help too:

~/unix.com$ date -d '7 days ago'
2 Likes

Hi Corona688,

Thanks for your reply.

But I am getting an error and couldn't figure this out.

#!/bin/sh

# Get epoch seconds
USERDATE=$(date +%s -d 20110701)
# Subtract 1 week
((USERDATE -= (60*60*24*7) ))

# Convert from epoch seconds into YYYYMMDD.
# -d assumes strings beginning with @ are epoch seconds.
D=$(date -d "@$USERDATE" +"%Y%m%d")
echo "One week previous was $D"
$ ./date.sh
date: invalid date `@1308888000'

One week previous was

Hmmm. That's weird. It works fine here, pasted keystroke for keystroke, and I've not encountered a version of GNU date before that didn't have it. You definitely have a working -d option, since the epoch time it printed is correct for that date.

What does date --version show you? I've got 8.5.

Maybe it just doesn't have @ for epoch seconds? That can be worked around using the syntax tukuyomi noticed. Does date -d '20110701 - 7 days' work?

1 Like

Hi,

I have tested the below script in a different box and the version of date is :

$ date --version
date (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Perfect this is working now:

#!/bin/sh
Day=`date -d '20110701 - 7 days' +%Y%m%d`
echo $Day
 ./date.sh
20110624

But,

When I am trying to execute the same script in a different box:

$ date --version
date (coreutils) 4.5.3
Written by David MacKenzie.

Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Result:

$ ./date.sh
20110708

Not sure why this is happening but wanted to know.

Really appreciate your quick responses and Thank you very much.

That version of GNU date is 9 years out of date.

try this..

$ date +%Y%m%d
20110702
$ TZ=CST+144 date +%Y%m%d
20110626
$