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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get any date from today FAQ updated

Status
Not open for further replies.

PHV

MIS
Nov 8, 2002
53,708
FR
The Faq:822-4802 now preserve the TimeZone abbreviation.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
How do I get any date relative to today (in ksh)
faq822-4802

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
  eval $(echo $TZ | sed '
s!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
  TZ=$zon1$((localOffset-24*nDays))$zon2 date $format
}
#-- Examples:
Code:
echo "Yesterday: $(GetDate -1)"
echo "Tomorrow: $(GetDate 1)"
echo "Next week: $(GetDate 7 '+%Y-%m-%d')"
#-- Note:
On some *nix flavor the authorized displacement is limited to +/-168 hours and thus the above "Next week" test fails.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

I am currently using this function but have found a problem with it. The check was made at 12.30 last night. However our clocks have gone forward last Sunday and believe this could be the problem.

Can you let me know where I should look on our server to see if there is another file or setting which may have cause the problem.

When I use the date funtcion it is returning the correct time.
 
I also meant to add that I am based in the UK. I am fairly certain the problem is with the localoffset function which uses time set at UTC. This is currently one hour behind.

Any help would be greatly appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top