Languages and Scripting

# get date of last week with script

SOLVED
Go to solution

## get date of last week with script

Dear all
i want to get all date of last week with script.
today is 05/04/2010
Begin date will be Mon - 04/26/2010
Tue - 04/27/2010
Wed - 04/28/2010
Thu - 04/29/2010
Fri - 04/30/2010
Sat - 05/01/2010
End date will be Sun - 05/02/2010

how can i make a script.
help me

Thanks
12 REPLIES 12
Trusted Contributor

## Re: get date of last week with script

This will be very easy if you convert today date to epoch time,
then minus 7 days, then convert the result back to normal date/time

Gudluck
Prasanth
Acclaimed Contributor
Solution

## Re: get date of last week with script

Hi:

If you wanted the last seven days, you could do:

# perl -le '\$t=time();for (\$d=\$t-(7*86400);\$d<=\$t;\$d+=86400) {print scalar localtime(\$d)}'

Regards!

...JRF...

## Re: get date of last week with script

James
Thank you very much.......

## Re: get date of last week with script

Oh I'm sorry.
I don't wanted the last seven days from today
i wanted all date of last week.
Get date => monday~Sunday of last week.

Good luck.
Trusted Contributor

## Re: get date of last week with script

#!/usr/bin/ksh

Current_time_epoch=`perl -e "print time "`
Week_day=`date +%a`

#days to be minus to get last sunday
# IF today is
# Sunday = 7days
# monday = 8days
# teusday= 9days
# Wednesday= 10days
# Thursday= 11days
# friday= 12days
# saturday= 13days

case "\$Week_day" in
Sun ) Sec_to_last_sunday=` echo 7 | awk '{print \$1*24*60*60}'` ;;
Mon ) Sec_to_last_sunday=` echo 8 | awk '{print \$1*24*60*60}'` ;;
Tue ) Sec_to_last_sunday=` echo 9 | awk '{print \$1*24*60*60}'` ;;
Wed ) Sec_to_last_sunday=` echo 10 | awk '{print \$1*24*60*60}'` ;;
Thu ) Sec_to_last_sunday=` echo 11 | awk '{print \$1*24*60*60}'` ;;
Fri ) Sec_to_last_sunday=` echo 12 | awk '{print \$1*24*60*60}'` ;;
Sat ) Sec_to_last_sunday=` echo 13 | awk '{print \$1*24*60*60}'` ;;
esac

let "Last_Sunday_epoch = \$Current_time_epoch - \$Sec_to_last_sunday"
let "Last_Mon_epoch = \$Last_Sunday_epoch + 86400"
let "Last_Tue_epoch = \$Last_Mon_epoch + 86400"
let "Last_Wed_epoch = \$Last_Tue_epoch + 86400"
let "Last_Thu_epoch = \$Last_Wed_epoch + 86400"
let "Last_Fri_epoch = \$Last_Thu_epoch + 86400"
let "Last_Sat_epoch = \$Last_Fri_epoch + 86400"

############Create a tmplate script ######

echo '#!/usr/bin/perl
\$time = localtime(\$ARGV[0]);
print "\$time\n";

' > /tmp/epoch_to_normal.pl

chmod 755 /tmp/epoch_to_normal.pl
#############################################

/tmp/epoch_to_normal.pl \$Last_Sunday_epoch
/tmp/epoch_to_normal.pl \$Last_Mon_epoch
/tmp/epoch_to_normal.pl \$Last_Tue_epoch
/tmp/epoch_to_normal.pl \$Last_Wed_epoch
/tmp/epoch_to_normal.pl \$Last_Thu_epoch
/tmp/epoch_to_normal.pl \$Last_Fri_epoch
/tmp/epoch_to_normal.pl \$Last_Sat_epoch

Try
Trusted Contributor

## Re: get date of last week with script

Hi,

your expected output is like this ????

[root@vm1 ~]# ./get_last_week_days.sh
Sun May 2 08:34:10 2010
Mon May 3 08:34:10 2010
Tue May 4 08:34:10 2010
Wed May 5 08:34:10 2010
Thu May 6 08:34:10 2010
Fri May 7 08:34:10 2010
Sat May 8 08:34:10 2010

Gudluck
Prasanth

## Re: get date of last week with script

Wow Thank you for reply!!! :-)
Acclaimed Contributor

## Re: get date of last week with script

Hi (again):

...or all in Perl:

# perl -le '\$t=time();\$d=7+(localtime)[6];\$b=\$t-(\$d*86400);for (0..6) {print scalar localtime(\$b+(\$_*86400))}'
Sun May 2 09:34:33 2010
Mon May 3 09:34:33 2010
Tue May 4 09:34:33 2010
Wed May 5 09:34:33 2010
Thu May 6 09:34:33 2010
Fri May 7 09:34:33 2010
Sat May 8 09:34:33 2010

Regards!

...JRF...