Computing.Net > Forums > Unix > how to calculate date (urgent)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to calculate date (urgent)

Reply to Message Icon

Name: Jonny Schulz
Date: July 24, 2003 at 03:03:32 Pacific
OS: Dynix/ptx
CPU/Ram: 64
Comment:

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.28

how can i do calculate?
i know that it is not possible with
the date command!

thanks a lot
jonny schulz




Sponsored Link
Ads by Google

Response Number 1
Name: Phil
Date: July 24, 2003 at 03:15:00 Pacific
Reply:

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.


0

Response Number 2
Name: WilliamRobertson
Date: July 24, 2003 at 03:55:12 Pacific
Reply:

Wow! Never knew you could do that!

That allows a date arithmetic script like:

#!/bin/ksh

integer days=$1
format=$2

if (( days < 1 ))
then
sign="+"
(( days *= -1 ))
else
sign="-"
fi

TZ=${TZ}${sign}$(( 24 * days ))

date ${format}


0

Response Number 3
Name: Jonny
Date: July 24, 2003 at 04:52:43 Pacific
Reply:

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?


0

Response Number 4
Name: nails
Date: July 24, 2003 at 09:19:58 Pacific
Reply:

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


0

Response Number 5
Name: WilliamRobertson
Date: July 24, 2003 at 09:48:24 Pacific
Reply:

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.



0

Related Posts

See More



Response Number 6
Name: WilliamRobertson
Date: July 24, 2003 at 16:45:11 Pacific
Reply:

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.


0

Response Number 7
Name: Jonny Schulz
Date: July 28, 2003 at 05:00:49 Pacific
Reply:

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


0

Response Number 8
Name: WilliamRobertson
Date: July 28, 2003 at 06:21:12 Pacific
Reply:

OK here's that link again:

shift_date $(date +%Y%m%d) -1


0

Response Number 9
Name: WilliamRobertson
Date: August 9, 2003 at 16:16:07 Pacific
Reply:

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') -10

which gives (today being 20030809)
20030730

If 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?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: how to calculate date (urgent)

How to check date in unix www.computing.net/answers/unix/how-to-check-date-in-unix/3115.html

How to calculate the time interval www.computing.net/answers/unix/how-to-calculate-the-time-interval-/7780.html

how to calculate days between dates www.computing.net/answers/unix/how-to-calculate-days-between-dates/7765.html