Convert string array to datetime
5 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Guillaume
2019-7-31
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!