Message Boards Message Boards

0
|
4385 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Import string dates using US date format?

Posted 5 years ago

I have imported a log file that contains dates in (M/D/Y) format. Because the dates are in June,

DateObject[#]&

Reports that the date string is ambiguous. How might I tell the Notebook - not the whole mathematica system - the date format of the file?

I tried

DateObject[#, DateFormat -> {"Month", "/", "Day", "/", "Year"}] & 

But it didn't have any effect on the warming messages.

Thanks Andy

POSTED BY: Andrew Burnett
4 Replies
Posted 5 years ago

That's great. Thank you very much.

POSTED BY: Andrew Burnett
Posted 5 years ago

Andrew,

The pattern has to match exactly

d = DateObject[{"06/11/2019 10:55:44 AM", {"Month", "/", "Day", "/", 
    "Year", " ", "Hour12", ":", "Minute", ":", "Second", " ", "AMPM"}}];
DateString[d, "ISODateTime"]
(* 2019-06-11T10:55:44 *)

Change AM to PM

d = DateObject[{"06/11/2019 10:55:44 PM", {"Month", "/", "Day", "/", 
    "Year", " ", "Hour12", ":", "Minute", ":", "Second", " ", 
    "AMPM"}}];
DateString[d, "ISODateTime"]
(* 2019-06-11T22:55:44 *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Hi Rohit, Thanks for your answer. Unfortunately, I can't get it to work, and I am obviously doing something dumb. Just to be clear, I am importing a csv log file that contains dates in the format 06/11/2019 10:55:44 AM. I want to convert them to DateObjects. The conversion does work, but it complains that it is ambiguous. Adding the format list, as you suggested, produces a "cannot be interpreted as a date." message

Here is the code snippet - can you see what I am doing wrong?

ecicc = Import["/Users/andy/Downloads/participants_497478659.csv"]; In[65]:= ecicc[[2, 3]] Out[65]= "06/11/2019 10:55:44 AM" ecicc[[2 ;; All, 3]] = DateObject[{#, {"Month", "/", "Day", "/", "Year", "hour", ":", "Minute", ":", "Seconds"}}] & /@ ecicc[[2 ;; All, 3]]; ecicc[[2 ;; All, 4]] = DateObject[{#, {"Month", "/", "Day", "/", "Year", "hour", ":", "Minute", ":", "Seconds"}}] & /@ ecicc[[2 ;; All, 4]]; DateObject::str: String 06/11/2019 10:58:51 AM cannot be interpreted as a date. General::stop: Further output of DateObject::str will be suppressed during this calculation. DateObject::str: String 06/11/2019 12:54:08 PM cannot be interpreted as a date. General::stop: Further output of DateObject::str will be suppressed during this calculation.

POSTED BY: Andrew Burnett
Posted 5 years ago

Hi Andrew,

To parse a date string in M/D/Y format:

d = DateObject[{"6/10/2019", {"Month", "/", "Day", "/", "Year"}}]
DateString[d, "ISODateTime"]
(* 2019-06-10T00:00:00 *)
POSTED BY: Rohit Namjoshi
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