Re: split a number to Date format

• To: mathgroup at smc.vnet.net
• Subject: [mg124253] Re: split a number to Date format
• From: Tomas Garza <tgarza10 at msn.com>
• Date: Sat, 14 Jan 2012 17:12:08 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <201201140754.CAA01282@smc.vnet.net>

You have to use a series of string operations, such as ToString, Characters, StringJoin, and others such as Partition and Prepend, all of which are really quite basic in Mathematica. First convert your time stamp (call it "num") into a string, split it into its individual characters, and group them into pairs. Then add a "20" to the first pair and join this to the remaining pairs. Convert this into an expression (i.e., from a string into an ordinary Mathematica expression; a list of numbers). Then use DataString and you're done. This said,
In[1]:= num = 111205020000;
Check first step:
In[2]:= Partition[Characters[ToString[num]], 2]
Out[2]= {{"1", "1"}, {"1", "2"}, {"0", "5"}, {"0", "2"}, {"0",   "0"}, {"0", "0"}}
Check second step:
In[3]:= StringJoin[{"2", "0"},  First[Partition[Characters[ToString[no]], 2]]]
Out[3]= "2011"
Then put everything together in a one-liner
In[4]:= DateString[ ToExpression /@   Prepend[StringJoin /@ Rest[Partition[Characters[ToString[no]], 2]],    StringJoin[{"2", "0"},     First[Partition[Characters[ToString[no]], 2]]]]]
Out[4]= "Mon 5 Dec 2011 02:00:00"
-Tomas

> Date: Sat, 14 Jan 2012 02:54:09 -0500
> From: cdmartin at me.com
> Subject: split a number to Date format
> To: mathgroup at smc.vnet.net
>
> I have experimental data in spreadsheet format.  The first column is a time stamp in the format 111205020000.
> If I could parse or split the number 111205020000 into {11,12,05,02,00,00} and replace the first 11 with 2011, the Date commands would work fine. I can do the replacing, but am having difficulty seeing how to split the number into  {11,12,05,02,00,00}.
>
> DateString[{2011, 12, 05, 02, 00, 00}] gives
> "Mon 5 Dec 2011 02:00:00"
>
> Any help to convert my number to the right Date format would be appreciated. I am using Mathematica 8.
>