Mathematica 8.0从字符串中解析DateList的有效方式,格式为yyyyMMddHHmmssSSS

mmo*_*ris 9 wolfram-mathematica

我有以下字符串:"20110103224832494"它采用以下格式:yyyyMMddHHmmssSSS.yyyy是年份,MM是月份,dd是日,HH是小时,mm是分钟,ss是秒,SSS是毫秒.

我认为以下内容会起作用:

DateList[{"20110103224832494",  
         {"Year", "Month", "Day", "Hour", "Minute", "Second", "Millisecond"}}
]
Run Code Online (Sandbox Code Playgroud)

但回报:

DateString::str: String 20110103224832494 cannot be interpreted as a date in 
format {Year,Month,Day,Hour,Minute,Second,Millisecond}. >>
Run Code Online (Sandbox Code Playgroud)

如果这样有效,它会有效吗?

Bre*_*ion 10

使用:

DateList[{"20110103224832494",  
   Riffle[{"Year",
           "Month", 
           "Day",   
           "Hour", 
           "Minute", 
           "Second", 
           "Millisecond"}, ""]}]
Run Code Online (Sandbox Code Playgroud)

  • @ Mr.Wizard,实际上这就是我的要求.更具体地说,我问,为什么在没有分隔符时我需要分隔符?我明白为什么会令人沮丧. (3认同)
  • `DateList [{"20110103224832494",{"年","月","日","小时","分钟","第二","","毫秒"}}]`作品过后也发现了题. (2认同)
  • 为什么需要在其余的日期/时间规范中插入一个空字符串? (2认同)

Mr.*_*ard 9

已更正将毫秒与秒组合.

你确实指定了"有效",我相信这比DateList以下快两个数量级:

stringDynP[s_String, p_] :=
  StringTake[s, Thread@{{0}~Join~Most@# + 1, #} &@Accumulate@p]

toDateList[string_String] := 
  MapAt[#/1000` &, #, -1] &[
    FromDigits /@ stringDynP[string, {4, 2, 2, 2, 2, 5}]
  ]

toDateList["20110103224832494"]
Run Code Online (Sandbox Code Playgroud)
{2011, 1, 3, 22, 48, 32.494}

stringDynP我的"动态分区"功能的字符串改编.


Mathematica 7用户的警告:该DateList方法产生虚假结果:

{2011, 1, 12, 9, 23, 24.094}
Run Code Online (Sandbox Code Playgroud)

据推测,在版本8中,可以使用以下方法:

DateList[
 {"20110103224832494",
   {"Year", "Month", "Day", "Hour","Minute", "Second", "Millisecond"}},
 DateDelimiters -> None
]
Run Code Online (Sandbox Code Playgroud)

  • 看起来像版本7中的"DateList"中的错误.当您在规范中添加分钟时,似乎会感到困惑... (4认同)