我有以下字符串:“20110103224832494”它采用以下格式:yyyyMMddHHmmssSSS。 yyyy是年份,MM是月份,dd是日,HH是小时,mm是分钟,ss是秒,SSS是毫秒。
我认为以下内容会起作用:
DateList[{"20110103224832494",
{"Year", "Month", "Day", "Hour", "Minute", "Second", "Millisecond"}}
]
但返回:
DateString::str: String 20110103224832494 cannot be interpreted as a date in
format {Year,Month,Day,Hour,Minute,Second,Millisecond}. >>
如果有效,它会有效吗?
答案 0 :(得分:10)
使用:
DateList[{"20110103224832494",
Riffle[{"Year",
"Month",
"Day",
"Hour",
"Minute",
"Second",
"Millisecond"}, ""]}]
答案 1 :(得分: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"]
{2011, 1, 3, 22, 48, 32.494}
stringDynP
是my "Dynamic Partition" function的字符串改编。
DateList
方法产生虚假结果:{2011, 1, 12, 9, 23, 24.094}
大概在版本8中可以使用以下方法:
DateList[
{"20110103224832494",
{"Year", "Month", "Day", "Hour","Minute", "Second", "Millisecond"}},
DateDelimiters -> None
]