Anyone got a date math routine?

Kenneth Brody kenbrody at bestweb.net
Mon Dec 3 13:29:48 PST 2007


Quoting Jay R. Ashworth (Mon, 3 Dec 2007 15:58:08 -0500):

> Get the impression I'm coding this week...?
>
> Before I write it myself, does anyone have a fuzzy age routine?  I need
> something that can take 2 dates and 2 times, and subtract one from the
> other, and then round, so that what I get is "32m", "6h", "3d", "2w",
> "4M", etc.  That is, figure out what the largest unit of time is that
> the difference can be represented in as a clipped integer, and return
> that as a 3 character string.
>
> I have a design for such a routine in my head, but I figure if anyone's
> already plowed the ground...
>
> If not, I'll post it here when I'm done with it.

Well, here's my first pass, off the top of my head...

Given that this is to give approximate ages anyway, this will assume:

     One year is 365.25 days.
     One month is 1/12th of a year.

Use date/time math to get the number of seconds between the date+time.
(Date minus date equals days.  Time minus time equals hours.)  Then,
use the following:

     1 minute = 60 seconds
     1 hour = 3600 seconds
     1 day = 86,400 seconds
     1 week = 604,800 seconds
     1 month = 2,629,800 seconds
     1 year = 51,557,600 seconds

You then just need to decide cutoffs.  For example, how many seconds
do you need before switching to hours?  How many before switching to
days?  And so on.

Then, divide by that unit.

Plus, are you sure you want to clip and not do some rounding at least?
Is 51,557,599 seconds really "11 months"?

-- 
KenBrody at BestWeb dot net        spamtrap: <g8ymh8uf001 at sneakemail.com>
http://www.hvcomputer.com
http://www.fileProPlus.com


More information about the Filepro-list mailing list