Convert string array to datetime

5 Ansichten (letzte 30 Tage)
Liam Quantrill
Liam Quantrill am 31 Jul. 2019
Kommentiert: Liam Quantrill am 31 Jul. 2019
I am trying to convert a string in the format seen below into a datetime:
"2016-07-22 10:02:54.087216500-04:00"
I have had a look at the MATLAB documentation and tried the following:
datetime(ans, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSSSSSSSSXXX', 'TimeZone', 'America/New_York')
I receive the following error however:
"Unable to convert the text to datetime using the format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX'."
I am not actually sure that the timezone is New York, as I pulled the data from the internet and there was no indication of this. It was the only one in the list found in the MATLAB help under the "TimeZone" section of the "datetime" help that had -04:00 however, so I assumed that it would be this.
Can someone see where my mistake in understanding the format of the date is? If so please could you provide a solution that will help me read this format of string into a datetime?
Thanks!

Akzeptierte Antwort

Guillaume
Guillaume am 31 Jul. 2019
Yes, there's no 'T' in your input so the format should be 'yyyy-MM-dd HH:mm:ss.SSSSSSSSSXXX'
>> s = "2016-07-22 10:02:54.087216500-04:00";
>> datetime(s, 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSSSSSSSSXXX', 'TimeZone', 'America/New_York', 'Format', 'dd MMM yyyy HH:mm:ss z')
ans =
datetime
22 Jul 2016 10:02:54 EDT
I'm overriding the display format in the above. This is of course optional.
  1 Kommentar
Liam Quantrill
Liam Quantrill am 31 Jul. 2019
Ah ok, my bad for not spotting that... Thanks for your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by