Hello Forum,
I want to retrieve the old date from "date" command.
For example: It's 02 Nov today and i want 7 days old date say 25 oct
then how can i do that???
As i do
date | awk '{print $3-7}'
It returns -5 (As 2-7)
So can anyone help me to solve this out..... I think by making function, it would be easier but i want to do this by making One liner command as i mentioned.....
Any kind of help and suggession would be appreciated.....
Many Thanks
Lokesh
vino
November 2, 2007, 6:16am
2
If you have GNU date, then
date --date "7 days ago"
I think the Perderabo's datecalc can also handles these requests.
Hi,
I couldnot think of a one-liner command as u said.
Please find below a script, which calculates previous date..i.e current date-1
Can you please check, if modifying this script a bit will work in your case
Script below
---------------
day=`date +%d%`
month=`date +%m%`
year=`date +%Y`
daynm=`date +%a`
case "$daynm" in
"Mon"\) day=\`expr "$day" - 3\`
k=3;;
"Sun"\) day=\`expr "$day" - 2\`
k=2;;
*\) day=\`expr "$day" - 1\`
k=1;;
esac
if test $day -le 0
then
month=\`expr "$month" - 1\`
case "$month" in
0\)
month=12
year=\`expr "$year" - 1\`
;;
esac
m=\`expr $day - 1\`
day=\`cal $month $year | grep . | fmt -1 | tail $m | head -1\`
fi
if [[ "$\{day\}" = [0-9] ]]
then
day="0"$day
fi
if [[ "$\{month\}" = [0-9] ]]
then
month="0"$month
fi
Final_Date=$year$month$day
echo $Final_Date
Thanks ,
Suresh Sampat
gus2000
November 5, 2007, 10:51am
4
You can print the date if you're looking for "hours earlier/later than right now" by manipulating the TZ variable:
# date
Mon Nov 5 09:47:32 CST 2007
# TZ=$(((6+24*7))) date
Mon Oct 29 09:47:37 2007
It's a bit cheap but it works. Note that I needed to start with 6 for my local timezone, and then add hours to go backward (7 days of 24 hours each).