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!

Getting date one month before today using Unix shell Script 2

Status
Not open for further replies.

RajShekar

Programmer
May 27, 2004
14
0
0
US
I want to get date one month before today using unix shell script in YYYYMMDD format?
 
The thread Thread80-850019 is using perl, but I would like a solution without using perl and instead using only UNIX script.
 
Take a look at my faq: faq822-4802
(How do I get any date relative to today (in ksh))

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
<self-promotion>

FAQ822-4802 had a ksh solution as well

</self-promotion>

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Sorry if I got it wrong. I used this function and it gives me the same date.

Script:
Code:
#!/usr/bin/sh

# GetDate nDays [format]
GetDate ()
{ 
set -x

  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
}
 
echo "Yesterday: $(GetDate -1)"
echo "Tomorrow: $(GetDate 1)"
echo "Next week: $(GetDate 7 '+%Y-%m-%d')"

O/P Debugged
Code:
prompt>sh date.sh
+ typeset -i nDays=-1
+ format=
+ sed s![^-0-9]*\([-0-9]*\).*!\1!
+ echo EST5EDT
+ typeset -i localOffset=5
+ date
+ TZ=X29
Yesterday: Thu Jun 10 09:16:42 EDT 2004
+ typeset -i nDays=1
+ format=
+ echo EST5EDT
+ sed s![^-0-9]*\([-0-9]*\).*!\1!
+ typeset -i localOffset=5
+ date
+ TZ=X-19
Tomorrow: Thu Jun 10 09:16:42 EDT 2004
+ typeset -i nDays=7
+ format=+%Y-%m-%d
+ echo EST5EDT
+ sed s![^-0-9]*\([-0-9]*\).*!\1!
+ typeset -i localOffset=5
+ date +%Y-%m-%d
+ TZ=X-163
Next week: 2004-06-10

 
What do you consider a month? 28 days? Or will you be running your script for days 1-28 only, and hence the day in the month can coincide (28th March - 1 month = 28th Feb)?
 
This function is to to get any date in the past or in the future from today. So -1 is used to get yesterday's date.
1 for tomorrow's date .....
 
What 'glue' commands can you use. For example, can you use:
* java
* cal
* perl
* awk
 
+ TZ=X-19
Tomorrow: Thu Jun 10 09:16:42 EDT 2004

Should be:
+ TZ=X-19
Tomorrow: Thu Jun 1[highlight]1[/highlight] 09:16:42 [highlight]X[/highlight] 2004
This function works for me and apparently for most other people.
What is your context (OS, shell) ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
O.S is HP-UX 11i. I don't exactly know what shell I am using. When I say which shell, it returns /usr/bin/sh. And as export name=value is valid in my scripts I assume it is korn.
 
In the man page for date is TZ mentioned ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The date command displays or sets the current HP-UX system clock date and time. Since the HP-UX system operates in Coordinated Universal Time (UTC), date automatically converts to and from local standard or daylight/summer time, based on your TZ environment variable.

TZ determines the conversion between the system time in UTC and the time in the user's local time zone. See environ(5) and tztab(4). TZ also determines the content (that is, the time-zone name produced by the %z and %Z directives) of date and time strings output by the date command.
 
And what is displayed when type this at your shell prompt:
TZ=X-19 date

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Ah, very interesting as I guessed the HP-UX shell was Posix compliant ...
And what is displayed when type this at your shell prompt:
(export TZ=X-19; date)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top