Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i need help! can someone help me please?
i try to calculate date under unix (ksh)...example:
todays date: 2003.07.23
date-5
= 2003.07.28how can i do calculate?
i know that it is not possible with
the date command!thanks a lot
jonny schulz

Jonny
You can use the following, changing to the appropriate time zone. Check out the man page for date:
TZ=GMT-80 date +%Y.%m.%d
If putting in a script run within ` ` (not'')
Phil.

Wow! Never knew you could do that!
That allows a date arithmetic script like:
#!/bin/ksh
integer days=$1
format=$2if (( days < 1 ))
then
sign="+"
(( days *= -1 ))
else
sign="-"
fiTZ=${TZ}${sign}$(( 24 * days ))
date ${format}

thanks many for your help but unfortunately doesn't run.
I get the answer "GMT-80 not found".Be aimed my is:
we have todays date: 2003.07.24 (date +'%Y.%m.%d')
now I start a script: test1.sh 30 (30 = $1)
I want to get back: 2003.06.28 (with echo $xxx)how can I realize this?

Jonny:
Dealing with dates in unx is pain. We've dealt with this before. Look at:
http://www.computing.net/unix/wwwboard/forum/5223.html
Regards,
Nails

What is test1.sh?
If you have a script "add_days" exactly like the one I posted above (using Phil's awesome Timezone modifier), then
add_days -30
should give (today being 24th July):
Tue Jun 24 16:42:02 GB 2003
You can add your format mask:
add_days -30 +'%Y.%m.%d'
2003.06.24
This works on Solaris, maybe it won't on Dynix.
Alternatively follow Nails' link - I wrote the "shift_date" script before I heard of the TZ trick.

Hmm, TZ doesn't seem to exist on FreeBSD (just tried). Maybe you can only modify the timezone at runtime this way on particular versions of Unix.

oh oh oh
its very hard to work with dates under unix, because there is no spezify tool for it!
my solution is, that "we" have a oracle server installed on the unix machine, so i can calculate the date with sql:
backup_datum=`sqlplus -s EOF
user/password
set head off
set feed off
select to_char(sysdate - $1 ,'YYYYMMDD') from dual;
EOF`this is another way to do it!
william: test1.sh is means only
name of the script: test1
writing in: sh (shell) :-)regards
Jonny

Jonny,
You posted an update to the earlier thread #5223, including your "calculate_date.sh" script, but this forum won't let me post to it because it's too old, so I'll try to continue here.Well that's one heck of a script.
I haven't worked out what it all does yet, but it did happen to notice the leap year calculation is wrong (it just tests whether $year % 4 is zero - not the same thing!)
However, the original question was:
> how do I get today's date minus 'x' into a variable of the form 'YYYYMMDD'?
Well, today's date in the format YYYYMMDD is
date +'%Y%m%d', e.g. 20030809.and to get that date minus 10 (say) using my shift_date script (see link above), just enter:
shift_date $(date +'%Y%m%d') -10which gives (today being 20030809)
20030730If you want that in a variable for naming files, just use
archive_date=$(shift_date $(date +'%Y%m%d') -10)
Is that what you need, or am I missing something?

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |