Azzera filtri
Azzera filtri

Another Datetime conversion problem!

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 9 Ott 2019
Modificato: Stephen23 il 9 Ott 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 il 15 Ott 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.

Accedi per commentare.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by