필터 지우기
필터 지우기

Another Datetime conversion problem!

조회 수: 1 (최근 30일)
Steven Brace
Steven Brace 2019년 10월 9일
댓글: Peter Perkins 2019년 10월 15일
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