Another Datetime conversion problem!

2 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 9 Oct 2019
Edited: Stephen23 on 9 Oct 2019
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 on 15 Oct 2019
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.

Sign in to comment.

Accepted Answer

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

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!