Effacer les filtres
Effacer les filtres

Another Datetime conversion problem!

1 vue (au cours des 30 derniers jours)
Steven Brace
Steven Brace le 9 Oct 2019
Commenté : Peter Perkins le 15 Oct 2019
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 commentaires
Stephen23
Stephen23 le 9 Oct 2019
Modifié(e) : Stephen23 le 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 le 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.

Connectez-vous pour commenter.

Réponse acceptée

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

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by