Computing.Net > Forums > Unix > How to check date in unix

How to check date in unix

Reply to Message Icon

Original Message
Name: Samson
Date: April 16, 2002 at 01:23:37 Pacific
Subject: How to check date in unix
Comment:

Anybody knows how to get what date was 14 days ago of the current system date.


Report Offensive Message For Removal

Response Number 1
Name: James Boothe
Date: April 16, 2002 at 07:29:46 Pacific
Subject: How to check date in unix
Reply: (edit)

I ran across a neat solution a while back (posted by "mib") that computed yesterday's date. Here is my version, based on that logic, which goes back multiple days. It can go back to beginning of previous month, so depending on current date, it could go back 28 to 61 days. Script is currently hard-coded to go back 14 days, which it can always do. If changed to go back more than 28 days, no logic is present to ensure that you went back prior to last month.

#!/usr/bin/ksh
back=14
typeset -Z2 DAY
date '+%m %d %Y' | read MONTH DAY YEAR
((DAY=$DAY-$back))
if [ $DAY -lt 1 ] ; then
((MONTH=$MONTH-1))
if [ $MONTH -eq 0 ] ; then
MONTH=12
((YEAR=$YEAR-1))
fi
((TAIL=$DAY*-1+1))
DAY=`cal $MONTH $YEAR | fmt -1 |
tail -n $TAIL | head -n 1`
fi
echo "Back $back was: $MONTH $DAY $YEAR"
exit 0


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to check date in unix

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software