Re: list of dates
- To: mathgroup at smc.vnet.net
- Subject: [mg88354] Re: list of dates
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Fri, 2 May 2008 06:03:40 -0400 (EDT)
- Organization: University of Bergen
- References: <fvegu1$5e9$1@smc.vnet.net>
Roger Nye wrote:
> Hi,
> I have a list of dates in the format YYYY-MM-DD and I want to convert them
> to a decimal value, so 1963-01-01 goes to 63.0 and 1985-07-01 goes roughly
> to 85.5.
>
> Any suggestions on the best way to do this?
It is not possible to do this in a precise way because years don't pass
"linearly". Leap years are longer than non-leap years. So we have to
make a compromise: if y1, y2, y3, and y4 are the floating point numbers
you use to represent dates, it will either not be true that if (y2-y1)
== (y4 - y3) then the time interval between y1 and y2 has the same
length as the interval between y3 and y4; or the date 19AB-01-01 might
not be converted to the exact integer AB.
I chose the latter compromise, and wrote this function:
dateToFloat[date_String] :=
N[100 DateDifference[{1900, 1, 1}, DateList[date]]/
DateDifference[{1900, 1, 1}, {2000, 1, 1}]]
dateToFloat["1963-01-01"]
62.9997
dateToFloat["1985-07-01"]
85.4972
If you use different anchors than 1900-01-01 and 2000-01-01, then you'll
get slightly different results.
I hope this helps,
Szabolcs