Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

@DateDiff

25 views
Skip to first unread message

lcaverly

unread,
Feb 18, 2009, 4:34:02 PM2/18/09
to
Hi,
Does anyone have a lean user-defined function or batch file to
calculate the difference between two dates? For example;

Paul McCartney was born on June 18, 1942.

As of today, he would be 66 years, 8 months, 0 days old.

Maybe something like @datediff[1942-06-18, %_isodate] and returns
66-08-00

Thanks,

Joe

CBFalconer

unread,
Feb 18, 2009, 10:51:43 PM2/18/09
to

Won't be simple. For example, how long is a year? 365, 366, or
365 1/4 days. You have to worry about all leap years (including
2000), and non-leap years (1900, 2100). What calendar are you
using, and when did it go into effect. How long is a month, 28,
29, 30, 31 days?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Klaus Meinhard

unread,
Feb 19, 2009, 3:52:04 AM2/19/09
to
lcaverly wrote:

4DOS can use and do aritmetic with dates in the DOS range, starting
1980-01-01. The function @date gives you the number of days since
1980-01-01, so that you could easily compare 2 dates this way.

I don't know of any lean and easy way to do date arithmetic with dates
prior to 1980-01-01 with 4DOS, however.

--
Mit freundlichem Gruß,

Klaus Meinhard


Michael Bednarek

unread,
Feb 19, 2009, 9:13:22 AM2/19/09
to
On Thu, 19 Feb 2009 23:52:05 +1000, Michael Bednarek wrote in comp.os.msdos.4dos:

>On Wed, 18 Feb 2009 13:34:02 -0800 (PST), lcaverly wrote in
>comp.os.msdos.4dos:


>
>>Hi,
>> Does anyone have a lean user-defined function or batch file to
>>calculate the difference between two dates? For example;

[snip]
>I found this algorithm (see in code) and ported it to 4NT. Instead of
>using the Unix epoch for date arithmetic, I used a batch file which I
>had already lying around, JULIAN.BTM
[snip]
> :: Age.BTM Calculate age in years, months, days
[snip]
> :: JULIAN.BTM Convert Gregorian Date to Julian Day Number
[snip]

PS: You can check results against <http://www.timeanddate.com/date/duration.html>;
note that input to that page can be parameterised in the URL, eg:
<http://www.timeanddate.com/date/durationresult.html?d1=18&m1=6&y1=1942&d2=18&m2=2&y2=2009>

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Michael Bednarek

unread,
Feb 19, 2009, 8:52:05 AM2/19/09
to
On Wed, 18 Feb 2009 13:34:02 -0800 (PST), lcaverly wrote in
comp.os.msdos.4dos:

>Hi,

I found this algorithm (see in code) and ported it to 4NT. Instead of


using the Unix epoch for date arithmetic, I used a batch file which I
had already lying around, JULIAN.BTM

It's not extensively tested, but your test case works. I tried to make
it 4DOS (7.50 under Win 5.1)compatible, although I don't use that
anymore; however, 4DOS chokes with "Out of environment space" in line
28 (the long one) - left to the discerning reader to fix; this doesn't
happen in 4NT (6.01).

@ECHO OFF
*SETLOCAL


:: Age.BTM Calculate age in years, months, days

:: Author: Michael Bednarek (mb AT mbednarek.com)
:: Input: d1 m1 y1 d2 m2 y2
:: Output: Age in yyyy mm dd
:: Source: http://stackoverflow.com/questions/453208/how-can-i-calculate-the-age-of-a-person-in-year-month-days
:: (replaced FLOOR() with INT() to allow for 4DOS)
:: Needs JULIAN.BTM as a workaround to compute something similar to the Unix "epoch".

FUNCTION tm=`%@EXEC[@CALL JULIAN.BTM %1 %2 %3] %JULIAN`

SET d1=%1
SET m1=%2
SET y1=%3
SET d2=%4
SET m2=%5
SET y2=%6

SET t1=%@EVAL[%y1 * 12 + %m1 - 1]
SET t2=%@EVAL[%y2 * 12 + %m2 - 1]
SET dm=%@EVAL[%t2 - %t1]
IFF %d2 GE %d1 THEN
SET r=%@INT[%@EVAL[%dm / 12]] years, %@EVAL[%dm %% 12] months, %@EVAL[%d2 - %d1] days
ELSE
SET dm=%@EVAL[%dm - 1]
SET t2=%@EVAL[%t2 - 1]
SET r=%@INT[%@EVAL[%dm / 12]] years, %@EVAL[%dm %% 12] months, %@EVAL[%@tm[%d2 %m2 %y2] - %@tm[%d1 %@EVAL[%t2 %% 12 + 1] %@INT[%@EVAL[%t2 / 12]]]] days
ENDIFF
ECHO %r
UNSET d1 m1 y1 d2 m2 y2 r


@*Echo Off
*SETLOCAL


:: JULIAN.BTM Convert Gregorian Date to Julian Day Number

:: Author: Michael Bednarek (mb AT mbednarek.com)
:: Input: dd mm yyyy
:: Output: Echo DOW and JULIAN and return variable JULIAN
:: Method: From a German HP Calculator Manual
:: which says it only works for dates between 1-Mar-1900 and 28-Feb-2100
:: Seems to work longer, though.

:: Parameters are not checked!

:: bias to 1-Jan-4713 BC
Set bias=1720983

:: Prepare
Iff %2 gt 2 Then
Set y2=%3
Set m2=%@Eval[%2 + 1]
Else
Set y2=%@Eval[%3 - 1]
Set m2=%@Eval[%2 + 13]
EndIff

:: This is it
Set JULIAN=%@Eval[%@Int[%@Eval[365.25 * %y2]]+%@Int[%@Eval[30.6001 * %m2]] + %1 + %bias]
Set DOW=%@Word[%@Eval[%JULIAN %% 7],Sun,Mon,Tue,Wed,Thu,Fri,Sat]

:: Clean up
Unset y2 m2 bias

:: Echo only if called from the command line
If %_BATCH eq 1 Echo %JULIAN %DOW
ENDLOCAL JULIAN DOW

Have fun.

lcaverly

unread,
Feb 19, 2009, 3:32:42 PM2/19/09
to
On Feb 19, 8:52 am, Michael Bednarek

<mbATmbednarek....@BLACKHOLESPAM.NET> wrote:
> I found this algorithm (see in code) and ported it to 4NT. Instead of
> using the Unix epoch for date arithmetic, I used a batch file which I
> had already lying around, JULIAN.BTM
>
> It's not extensively tested, but your test case works. I tried to make
> it 4DOS (7.50 under Win 5.1)compatible, although I don't use that
> anymore; however, 4DOS chokes with "Out of environment space" in line
> 28 (the long one) - left to the discerning reader to fix; this doesn't
> happen in 4NT (6.01).

Hi Michael,
I used your code as-is, and it worked great! No choking, no
coughing.

Thankyou very much. You are a scholar and a gentleman.

Joe

0 new messages