Why do "datenum" and "datestr" not give the same date after conversion?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 19 Apr. 2018
Bearbeitet: MathWorks Support Team
am 3 Jul. 2024
I am using the "datenum" function to create an object and get the date as a string using "datestr". I pass "20101231" (31-Dec-2010) as input to "datenum" but get back "31-Jan-2010" from "datestr" instead.
Following code shows how I am using the functions.
>> a = datenum('20101231','YYYYMMDD');
>> b = datestr(a)
b =
'31-Jan-2010 00:12:00'
Akzeptierte Antwort
MathWorks Support Team
am 25 Jun. 2024
Bearbeitet: MathWorks Support Team
am 3 Jul. 2024
The issue happens because "MM" is the format for 'minute' in "datenum".
You can use the lower case "mm" to get the expected output:
>> a = datenum('20101231','YYYYmmDD');
>> b = datestr(a)
b =
'31-Dec-2010'
To avoid the future confusion, we highly recommend using "datetime" with more intuitive letter identifiers, for example
>> a = datetime('2010-12-31')
a =
datetime
31-Dec-2010
>> a = datetime('2010-12-31','InputFormat','yyyy-MM-dd')
a =
datetime
31-Dec-2010
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!