Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get any date relative to today (in ksh)

Shell

How do I get any date relative to today (in ksh)

by  PHV  Posted    (Edited  )
Here a simple ksh function to get any date in the past or in the future from today.
It takes two arguments, the first is mandatory and is the number of days from today (negative for backward) and the second one is optional and is the format you want the date command uses.
Code:
GetDate(){ # GetDate nDays [format]
  typeset -i nDays=$1; format=$2
  typeset -i localOffset=$(echo $TZ |
   sed 's![^-0-9]*\([-0-9]*\).*!\1!')
  TZ=X$((localOffset-24*nDays)) date $format
}
Examples:
Code:
echo "Yesterday: $(GetDate -1)"
echo "Tomorrow: $(GetDate 1)"
echo "Next week: $(GetDate 7 '+%Y-%m-%d')"
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top