Message Boards Message Boards

0
|
939 Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Date String "20240811222725" cannot be interpreted as a date?

Posted 4 months ago

Instruction:

FromDateString["20240811222725", {"Year", "Month", "Day", "Hour", "Minute", "Second"},TimeZone->"UTC"]

Result:

FromDateString: String 20240811222725 cannot be interpreted as a date in the specified format.

What am I doing wrong?

POSTED BY: Steven Buehler
8 Replies

Try setting the delimiters explicitly, i.e.:

FromDateString["20240811222725", <|
  "Elements" -> {"Year", "Month", "Day", "Hour", "Minute", "Second"}, 
  "Delimiters" -> {""}|>, TimeZone -> "UTC"]
POSTED BY: Sascha Kratky

FromDateString command dosen't take your format date.

In Mathematica:

DATE = "20240811222725";

DateObject[{StringTake[DATE, {1, 4}] // ToExpression, 
  StringTake[DATE, {5, 6}] // ToExpression, 
  StringTake[DATE, {7, 8}] // ToExpression, 
  StringTake[DATE, {9, 10}] // ToExpression, 
  StringTake[DATE, {11, 12}] // ToExpression, 
  StringTake[DATE, {13, 14}] // ToExpression}]
 (*Gives date*)
POSTED BY: Mariusz Iwaniuk
Posted 4 months ago

Or, quicker:

ExternalEvaluate["Python","import datetime; datetime.datetime.strptime('20220811123456","%Y%m%d%H%M%S")"]
POSTED BY: Steven Buehler

the code can by simplified more:

DateObject[ToExpression@StringTake["20240811222725", Join[{{1, 4}}, Partition[Range[5, 14], 2]]]]

Regards M.I.

POSTED BY: Mariusz Iwaniuk
Posted 4 months ago

I think FromDateString expects at least some structure in the string to indicate that it's a date, specifically some delimiters.

POSTED BY: Eric Rimbey
Posted 4 months ago

Yuck. I guess I can call out to Python or the shell to reform it into a string that Wolfram can interpret…

POSTED BY: Steven Buehler
Posted 4 months ago

You could reform it within Mathematica.

POSTED BY: Eric Rimbey
Posted 4 months ago

Quicker (or, rather, easier for this amateur) to use an ExternalEvaluate of strptime() in Python than trying to split the string all up in Wolfram. :-)

POSTED BY: Steven Buehler
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract