Another Datetime conversion problem!

2 次查看(过去 30 天)
I am trying to import date and time information from a CDV which is in the following format:
2019-10-04T11:41:06+01:00
Looking at this documentation this should work:
t = datetime(DateStrings,'InputFormat','uuuu-MM-dd''T''HH:mmXXX','TimeZone','UTC')
So I have written this line:
convertDATETIME=datetime(temperatureDATA(:,1),'InputFormat','uuuu-MM-dd''T''HH:mm:ssXXX','TimeZone','GMT')
However for the above input I get this result:
'04-41-2019 11:41'
matlab dateWhy are the months and minutes both the same? I have tried changing the capitalisation of them and it doesn't improve the situation?
Many thanks,
Steve
  4 个评论
Stephen23
Stephen23 2019-10-9
编辑:Stephen23 2019-10-9
This is what I get when I try the code in your question (R215b):
>> c = '2019-10-04T11:41:06+01:00';
>> t = datetime(c,'InputFormat','uuuu-MM-dd''T''HH:mm:ssXXX','TimeZone','GMT')
t =
04-Oct-2019 10:41:06
>> t.Format
ans =
dd-MMM-uuuu HH:mm:ss
"result is different for InputFormat and Format"
Of course your examples produce different outputs:
  • 1st example: As you are not actually converting from string the InputFormat is simply ignored, and the datetime is displayed using the default Format.
  • 2nd example: you created a datetime object with a specific Format, so it is displayed using exactly the Format that you specified.
If you are not importing from strings, then the ImportFormat has no effect. The Format affects how the datetime object is displayed, but has no effect on importing or the actual date/time value that is stored in the object.
Peter Perkins
Peter Perkins 2019-10-15
Steve (not Stephen), it looks like you mixed up M and m somewhere -- one is months, the other is minutes. Your line of code looks right, and works for me, so you must have run something different from what you posted. In any case, it looks like you're all set now.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-10-9
编辑:Andrei Bobrov 2019-10-9
DateStrings = '2019-10-04T11:41:06+01:00';
convertDATETIME=datetime(DateStrings,'InputFormat',...
'uuuu-MM-dd''T''HH:mm:ssZ','TimeZone','+0000');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by