Convert string array to datetime

5 vues (au cours des 30 derniers jours)
Liam Quantrill
Liam Quantrill le 31 Juil 2019
Commenté : Liam Quantrill le 31 Juil 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!

Réponse acceptée

Guillaume
Guillaume le 31 Juil 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 commentaire
Liam Quantrill
Liam Quantrill le 31 Juil 2019
Ah ok, my bad for not spotting that... Thanks for your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by