Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: split a number to Date format

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124275] Re: split a number to Date format
  • From: "Hans Michel" <hmichel at cox.net>
  • Date: Sun, 15 Jan 2012 04:51:08 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201201140754.CAA01282@smc.vnet.net>

Derek:

filterTS[dt_String] := 
 Module[{y, m, d, h, mi, s}, {y, m, d, h, mi, s} = 
   Map[FromDigits, Partition[IntegerDigits[ToExpression[dt]], 2]];
  y = y + 2000;
  DateList[{y, m, d, h, mi, s}]
  ]

In[2]:= filterTS["111205020000"]

Out[2]= {2011, 12, 5, 2, 0, 0.}

Took advantage of the fact that the string format can be grouped by 2.

Hans

-----Original Message-----
From: Hans Michel [mailto:hmichel at cox.net] 
Sent: Saturday, January 14, 2012 11:01 AM
To: 'Derek Martin'; 'mathgroup at smc.vnet.net'
Subject: [mg124275] Re: split a number to Date format

The nice thing about Regular Expression is that it has been around a while
now and there is a good place to go find tried, tested and sometimes well
documented pattern matching strings. I was very pleased when WRI introduced
Regular Expression in Mathematica. Even with Mathematica's (in their words
"powerful") pattern matching, I learned Regular Expressions some 6-7 years
before Mathematica came out. So I feel more comfortable around Regular
Expressions. As far as I know, I don't see an equivalent publically
available library of Mathematica pattern matching.

So I found a Regular Expression on this site 

http://regexlib.com/REDetails.aspx?regexp_id=2641

This pattern match will match ISO 8601 date time format with or without the
delimiters. 

Try
StringCases["111205020000", 
 RegularExpression[
   "(\\d{2}|\\d{4})(?:\\-)?([0]{1}\\d{1}|[1]{1}[0-2]{1})(?:\\-)?([0-2]\
{1}\\d{1}|[3]{1}[0-1]{1})(?:\\s)?([0-1]{1}\\d{1}|[2]{1}[0-3]{1})(?::)?\
([0-5]{1}\\d{1})(?::)?([0-5]{1}\\d{1})"] -> "20$1,$2,$3,$4,$5,$6"]

or
d = "111205020000";
dl=
StringCases[d,RegularExpression["(\\d{2}|\\d{4})(?:\\-)?([0]{1}\\d{1}|[1]{1}
[0-2]{1})(?:\\-)?([0-2]{1}\\d{1}|[3]{1}[0-1]{1})(?:\\s)?([0-1]{1}\\d{1}|[2]{
1}[0-3]{1})(?::)?([0-5]{1}\\d{1})(?::)?([0-5]{1}\\d{1})"]->
"20$1,$2,$3,$4,$5,$6"];
dl

You may also translate the RE pattern matching to the faster Mathematica
String matching. I don't know how RE is implemented in Mathematica but I
would think it is probably slower than the older built in matching
techniques.

Also note that a 2 digit year poses a problem of what to use when the 2
digits is greater than current year.  When do you use "19" or "20" to
designate century?

The result is a list but it needs further processing to get to a DateList.
You asked for help. This is what I could do with the time I had.

I will come back to it for a complete solution if time permits.

Hans



-----Original Message-----
From: Derek Martin [mailto:cdmartin at me.com] 
Sent: Saturday, January 14, 2012 1:54 AM
To: mathgroup at smc.vnet.net
Subject: [mg124275] split a number to Date format

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.

Thanks in Advance
Derek





  • Prev by Date: Re: NDSolve output without interpolating function
  • Next by Date: Re: Can't use subscripted variables in function definition?
  • Previous by thread: Re: split a number to Date format
  • Next by thread: Re: split a number to Date format