Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

get date of last week with script

SOLVED
Go to solution
Highlighted
Kyuyong-Kwon
Advisor

get date of last week with script

Dear all
i'm mading a script
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
Prasanth V Aravind
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
James R. Ferguson
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...
Kyuyong-Kwon
Advisor

Re: get date of last week with script

James
Thank you very much.......
Kyuyong-Kwon
Advisor

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.
Please help me.

Good luck.
Prasanth V Aravind
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
Prasanth V Aravind
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
Kyuyong-Kwon
Advisor

Re: get date of last week with script

Wow Thank you for reply!!! :-)
James R. Ferguson
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...
Kyu-Yong Kwon
Frequent Advisor

Re: get date of last week with script

Dear James
i have a question.
i don't know perl script.
i want to change date format as below:
Mon May 3 10:42:43 2010 => 20100503

Please help me.
Thanks
James R. Ferguson
Acclaimed Contributor

Re: get date of last week with script

Hi (again):

> i want to change date format as below:
Mon May 3 10:42:43 2010 => 20100503

Use this:

# cat ./lastweek
#!/usr/bin/perl
use strict;
use warnings;
my $t = time();
my @t = localtime($t);
my $d = 7 + (@t)[6];
my $b = $t - ( $d * 86400 );
for ( 0 .. 6 ) {
$t = ( $b + ( $_ * 86400 ) );
@t = localtime( $b + ( $_ * 86400 ) );
printf "%s => %4d%02d%02d\n", scalar localtime($t), $t[5] + 1900,
$t[4] + 1, $t[3];
}
1;

...run as:

# ./lastweek
Sun May 2 11:01:06 2010 => 20100502
Mon May 3 11:01:06 2010 => 20100503
Tue May 4 11:01:06 2010 => 20100504
Wed May 5 11:01:06 2010 => 20100505
Thu May 6 11:01:06 2010 => 20100506
Fri May 7 11:01:06 2010 => 20100507
Sat May 8 11:01:06 2010 => 20100508

...

Please reopen this thread to assign points appropriately. See:

https://forums11.itrc.hp.com/service/forums/helptips.do?#41

https://forums11.itrc.hp.com/service/forums/helptips.do?#22

Regards!

...JRF...
Kyu-Yong Kwon
Frequent Advisor

Re: get date of last week with script

thanks a lot
James R. Ferguson
Acclaimed Contributor

Re: get date of last week with script

Hi (again):

> thanks a lot

I'm glad you're satisfied with the answers provided. The appropriate thing to do is to reopen this thread and assign points appropriately. See:

https://forums11.itrc.hp.com/service/forums/helptips.do?#41

https://forums11.itrc.hp.com/service/forums/helptips.do?#22

Regards!

...JRF...