Re: Converting Dates to Mathematica Format
- To: mathgroup at smc.vnet.net
- Subject: [mg52087] Re: [mg52070] Converting Dates to Mathematica Format
- From: János <janos.lobb at yale.edu>
- Date: Wed, 10 Nov 2004 04:45:31 -0500 (EST)
- References: <200411090637.BAA22852@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Nov 9, 2004, at 1:37 AM, Gregory Lypny wrote:
> Hello Everyone,
>
> Is there a simple way to convert dates imported as "Day/Month/Year" to
> the Mathematica format of {Year, Month, Day}? My dates are one column
> in a matrix of data. Mathematica appears to be treating them as
> strings. I used StringReplace to turn 15/10/2004 to 15,10,2004 but it
> remains a string, of course.
>
> Any suggestions would be most appreciated. My alternative is to import
> all of the dates as 20041510 so the ordinal ranking is maintained by
> date calculations would not be possible.
>
> Greg
>
>
I had a similar problem. I had time data in the format of "17:06:45".
I converted them into seconds to do calculation with them:
{ToExpression[StringTake[#[[
1]],{1,2}]]*3600+ToExpression[StringTake[#[[1]],{4,
5}]]*60+ToExpression[StringTake[#[[1]],{7,8}]],ToExpression[StringTake[#
\
[[2]],{1,2}]]*3600+ToExpression[StringTake[#[[2]],{4,5}]]*60+ToExpressio
n[
StringTake[#[[2]],{7,8}]]}
So you just have to slice the date and convert the parts with
ToExpression. So if I would have datum="15/10/2004" then I would do
something like:
In[181]:=
datum = "15/10/2004"
Out[181]=
"15/10/2004"
In[182]:=
stringToDate[d_String] :=
{ToExpression[StringTake[d,
{7, 10}]], ToExpression[
StringTake[d, {4, 5}]],
ToExpression[StringTake[d,
{1, 2}]]}
Then if I add an arbitrary time fraction to the converted datum
In[186]:=
Join[stringToDate[datum],
{1, 1, 1}]
Out[186]=
{2004, 10, 15, 1, 1, 1}
I am able to use it with FromDate
In[187]:=
FromDate[%]
Out[187]=
3306790861
So, if I have a vector of those dates
In[234]:=
dateVector = Table[StringJoin[
ToString[Random[Integer,
{1, 28}]], "/",
ToString[Random[Integer,
{1, 12}]], "/",
ToString[Random[Integer,
{2000, 2004}]]],
{i, 1, 50}];
and the modified converter function is:
In[235]:=
stringToDate2[d_String] :=
{ToExpression[StringTake[d,
StringPosition[d, "/"][[
2,1]] + {1, 4}]],
ToExpression[StringTake[d,
StringPosition[d, "/"][[
1,1]] +
If[StringPosition[d,
"/"][[2,1]] -
StringPosition[d,
"/"][[1,1]] == 3,
{1, 2}, {1, 1}]]],
ToExpression[StringTake[d,
StringPosition[d, "/"][[
1,1]] +
If[StringPosition[d,
"/"][[1,1]] == 3,
{-2, -1}, {-1, -1}]]]}
to account for single digit days and months :), then you can use Map to
convert all of them at once like:
In[236]:=
(stringToDate2[#1] & ) /@
dateVector
Out[236]=
{{2004, 12, 1}, {2004, 6,
26}, {2004, 5, 27},
{2003, 3, 2}, {2002, 9,
18}, {2002, 9, 12},
{2003, 3, 5}, {2003, 2,
23}, {2001, 7, 23},
{2000, 2, 22}, {2001, 4,
12}, {2001, 3, 12},
{2001, 4, 8}, {2003, 3,
10}, {2004, 10, 24},
{2000, 10, 18}, {2002, 8,
7}, {2000, 11, 10},
{2003, 3, 13}, {2002, 8,
25}, {2004, 6, 16},
{2004, 7, 13}, {2004, 2,
19}, {2003, 6, 17},
{2003, 5, 12}, {2001, 2,
27}, {2003, 10, 5},
{2000, 4, 18}, {2001, 6,
24}, {2003, 3, 10},
{2003, 3, 4}, {2003, 8,
28}, {2002, 10, 20},
{2001, 7, 7}, {2003, 4,
20}, {2004, 5, 19},
{2003, 7, 14}, {2001, 4,
1}, {2002, 12, 20},
{2003, 7, 28}, {2003, 6,
23}, {2003, 1, 14},
{2002, 7, 27}, {2004, 5,
11}, {2004, 2, 28},
{2001, 10, 13}, {2001, 12,
10}, {2000, 3, 12},
{2001, 7, 15}, {2001, 3,
24}}
I hope it helps.
János
----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)
- References:
- Converting Dates to Mathematica Format
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Converting Dates to Mathematica Format