Board index » delphi » Date conversion

Date conversion

Hi
I've subtracted 2 dates and gotten the number of days between them
Now I want to convert those number of days to years, months, days.

ex: if i come up with 25674 days. How many years, months & days is it.
Sounds simple. But it's driving me nuts.

Thanks for any input.

Chuck Paulus

 

Re:Date conversion


Quote
Chuck wrote:

> Hi
> I've subtracted 2 dates and gotten the number of days between them
> Now I want to convert those number of days to years, months, days.

> ex: if i come up with 25674 days. How many years, months & days is it.
> Sounds simple. But it's driving me nuts.

> Thanks for any input.

> Chuck Paulus

You are not looking for day arithmetic. Use the member functions for
TDate to access the year, month, day etc. (since the number of years
can vary with the dates - e.g. two dates a day apart could be a year
apart as well).
--
Bipin Prasad
Email: prasad-as...@worldnet.att.net
Web  : http://www.geocities.com/~bprasad

Re:Date conversion


DQpDaHVjayA8Y2h1Y2tAbmV0cml4Lm5ldD4gc2NocmVlZiBpbiBhcnRpa2VsIDwwMWJjMjM2NiQ4
MTBhM2YyMCQ3MTZlOGFjZEBkZWZhdWx0Pi4uLg0KPiBIaQ0KPiBJJ3ZlIHN1YnRyYWN0ZWQgMiBk
YXRlcyBhbmQgZ290dGVuIHRoZSBudW1iZXIgb2YgZGF5cyBiZXR3ZWVuIHRoZW0NCj4gTm93IEkg
d2FudCB0byBjb252ZXJ0IHRob3NlIG51bWJlciBvZiBkYXlzIHRvIHllYXJzLCBtb250aHMsIGRh
eXMuDQo+IA0KPiBleDogaWYgaSBjb21lIHVwIHdpdGggMjU2NzQgZGF5cy4gSG93IG1hbnkgeWVh
cnMsIG1vbnRocyAmIGRheXMgaXMgaXQuDQo+IFNvdW5kcyBzaW1wbGUuIEJ1dCBpdCdzIGRyaXZp
bmcgbWUgbnV0cy4NCj4gDQoNCiBsb29rIGF0IG15IGhvbWVwYWdlICBodHRwOi8vb3Vyd29ybGQu
Y29tcHVzZXJ2ZS5jb20vaG9tZXBhZ2VzL0ZfUmVpanNiZXJnZW4vcGFnZTIuaHRtDQogYW5kIGRv
d25sb2FkIA0KCQ0KCURhdGU6IEVlbiBEZWxwaGkgY29tcG9uZW50IHZvb3IgZGF0dW0gYmVyZWtl
bmluZ2VuLiANCg0KVGhpcyBjb21wb25lbnQgY2FuIG1ha2UgdGhlIGNvbnZlcnRpb24gZm9yIHlv
dS4NCg0KDQpGb25zIFJlaWpzYmVyZ2VuICAgICAgICAgRU1haWwgRm9ucy5SZWlqc2JlcmdlbkBr
b25iaWIubmwNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDA0NTEuMjMw
MUBjb21wdXNlcnZlLmNvbQ0KDQogaHR0cDovL291cndvcmxkLmNvbXB1c2VydmUuY29tL2hvbWVw
YWdlcy9GX1JlaWpzYmVyZ2VuDQoNCiBQcm9ncmFtbWVyICAiU29mdCBJZGVlIFNvZnR3YXJlIg0K
ICAgTGVpZHNjaGVuZGFtICAgTmV0aGVybGFuZHMNCiAgICAgVGVsICArMzEgIDcwIDMyNzg2MTkg
IA0KICAgICBUZWwgICszMSAgNiA1NCA3Njg0MzggIA0KDQog

Re:Date conversion


On 25 Feb 1997 23:07:32 GMT, "Chuck" <ch...@netrix.net> wrote:

Quote
>Hi
>I've subtracted 2 dates and gotten the number of days between them
>Now I want to convert those number of days to years, months, days.

>ex: if i come up with 25674 days. How many years, months & days is it.
>Sounds simple. But it's driving me nuts.

>Thanks for any input.

>Chuck Paulus

Do you know the MOD operator?

x    x MOD 3

1        1
2        2
3        0
4        1
5        2
6        0
7        1
http://ourworld.compuserve.com/homepages/TK_Boyd/homepage.htm
Freeware, shareware for IBM type pcs. Ideas for parents, teachers

Re:Date conversion


In article <01bc2366$810a3f20$716e8acd@default>, Chuck
<ch...@netrix.net> wrote

Quote
>Hi
>I've subtracted 2 dates and gotten the number of days between them
>Now I want to convert those number of days to years, months, days.

>ex: if i come up with 25674 days. How many years, months & days is it.
>Sounds simple. But it's driving me nuts.

>Thanks for any input.

>Chuck Paulus

In the November 1996 Scientific American, there's an article that refers
to a book Edward M Reingold's book "Calendrical Calculations" (Cambridge
University Press, 1996) and the books home page at
http://emr.cs.uiuc.edu/home/reingold/calendar-book/index.html

I hope this proves of some help.

--
Gary Jones <g...@bohr.demon.co.uk>
PGP public key available from servers
Key ID: 6AFBEAA1
Key fingerprint: FF F0 6C 8A AC 42 1D 63  20 F3 AE 22 6C E1 79 83

Re:Date conversion


Quote
>   "Chuck" <ch...@netrix.net> writes:
>  I've subtracted 2 dates and gotten the number of days between them
>  Now I want to convert those number of days to years, months, days.
>  ex: if i come up with 25674 days. How many years, months & days is it.
>  Sounds simple. But it's driving me nuts.

I guess it depends on where you start and where you end. But give or take a day or to it should be fairly easy.
But if you need an exact answer I guess you need to start from day 1 and then figure out how many days to the next same date the
year after, subtract that from your sum and keep going.

johan

Re:Date conversion


Quote
jo...@lindgren.pp.se wrote:

> >   "Chuck" <ch...@netrix.net> writes:
> >  I've subtracted 2 dates and gotten the number of days between them
> >  Now I want to convert those number of days to years, months, days.
> >  ex: if i come up with 25674 days. How many years, months & days is it.
> >  Sounds simple. But it's driving me nuts.

> I guess it depends on where you start and where you end. But give or take a day or to it should be fairly easy.
> But if you need an exact answer I guess you need to start from day 1 and then figure out how many days to the next same date the
> year after, subtract that from your sum and keep going.

> johan

I think that I've done this assigning the result to a TDateTime Variable
and looking ath the resulting date.

-------------------------------------------------------------
Desde la ciudad de Mxico (and willing to relocate to Canada)
Raul Surez
rasua...@spin.com.mx

Re:Date conversion


Quote
Raul Suarez <rasua...@spin.com.mx> wrote:
>jo...@lindgren.pp.se wrote:
...stuff deleted
>I think that I've done this assigning the result to a TDateTime Variable
>and looking ath the resulting date.

I'm sorry, i think you haven't done it.
When you pack a TDateTime in a longint, it will do it assuming
12 Month, each 31 (!) days.
If You calculate dates with this longint and unpack it afterwards, you
will get...  

...NONSENS.

The only way to calculate the time between days is is write a function
calulating a realy absolute time, perhaps in seconds since 1.1.1980
00:00 and one for the reverse way.

It's not so easy to do so, so i added my procedures, they should do
it (I hope so).

--------

const mon : array[1..13] of longint =
            ( 0, 31, 59, 90,120,151,181,212,243,273,304,334,365);
{           (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);}

procedure abstime(dt:datetime; var pt:longint);
var y1979 : longint;
begin
  with dt do begin
    if year<77 then inc(year,2000);
    if year<1900 then inc(year,1900);
    if (year=1979) then begin
      y1979:=(4*365+1)*86400;
      inc(year,4);
    end else y1979:=0;
    pt:=trunc((year-1980)*365.25-0.1)+mon[month]+day-1;
    if ((year mod 4)=0) and (month>2) then inc(pt);
    pt:=pt*86400+longint(hour)*3600+min*60+sec-y1979;
  end;
end;

procedure unabstime(pt:longint; var dt:datetime);
var temp,sj : longint;
begin
  with dt do begin
    year:=round(pt/366/24/3600)+1979;
    month:=1;
    day:=1;
    hour:=0;
    min:=0;
    sec:=0;
    temp:=0;
    while(temp<=pt) do begin
      inc(year);
      abstime(dt,temp);
    end;
    dec(year);
    abstime(dt,temp);
    if (year mod 4)=0 then sj:=1 else sj:=0;
    while ((month=1) and ((temp+(mon[succ(month)]   )*86400)<=pt)) or
          ((month>1) and ((temp+(mon[succ(month)]+sj)*86400)<=pt)) do
      inc(month);
    abstime(dt,temp);
    temp:=pt-temp;
    day:=temp div 86400;
    dec(temp,day*86400);
    inc(day);
    hour:=temp div 3600;
    dec(temp,longint(hour)*3600);
    min:=temp div 60;
    dec(temp,min*60);
    sec:=temp;
  end;
end;

--------

---------------------------------------------
mwe...@limmat.ch
http://www.limmat.ch/mweiss/
---------------------------------------------

Re:Date conversion


In article <5g6eee$...@d2o10.telia.com>, jo...@lindgren.pp.se writes

Quote
>>   "Chuck" <ch...@netrix.net> writes:
>>  I've subtracted 2 dates and gotten the number of days between them
>>  Now I want to convert those number of days to years, months, days.
>>  ex: if i come up with 25674 days. How many years, months & days is it.
>>  Sounds simple. But it's driving me nuts.

>I guess it depends on where you start and where you end. But give or take a day
>or to it should be fairly easy.
>But if you need an exact answer I guess you need to start from day 1 and then
>figure out how many days to the next same date the
>year after, subtract that from your sum and keep going.

>johan

Why not use DecodeDate?
Worth a try.
--
Mike Proud

Other Threads