detestr/datenum behavior on Jan 1st.

3 vues (au cours des 30 derniers jours)
Val Schmidt
Val Schmidt le 7 Mar 2017
Modifié(e) : James Tursa le 7 Mar 2017
I am going crazy. Please someone explain why this behavior occurs with dates on the 1st of Jan.
Here I want the MATLAB serial date associated with Jan 1, 2017 at mid-night:
datestr(datenum(2017,0,0,0,0,0))
ans =
31-Dec-2016
Ok so maybe this makes sense if you think of Midnight as part of the previous day. But I don't know many people who do. So what about an hour later:
datestr(datenum(2017,0,0,1,0,0))
ans =
31-Dec-2016 01:00:00
This result seems just plain wrong.
Interestingly MATLAB doesn't distinguish between a 0 or 1 to indicate January:
datestr(datenum(2017,1,0,1,0,0))
ans =
31-Dec-2016 01:00:00
In all of these cases, why does datestr() return a date on the previous day? What am I missing?
-Val

Réponse acceptée

James Tursa
James Tursa le 7 Mar 2017
Modifié(e) : James Tursa le 7 Mar 2017
The 0'th day of January is interpreted as 31st December of the previous year. Makes sense to me. E.g.,
>> datestr(datenum(2017,1,0,0,0,0)) % <-- 0'th day of January
ans =
31-Dec-2016 % <-- is 31st day of December previous year
>> datestr(datenum(2017,1,1,0,0,0)) % <-- 1st day of January
ans =
01-Jan-2017 % <-- yep
This is all as expected. The only part that doesn't make much sense to me is allowing a 0 or a 1 to represent January. I can't offhand find a reference to that behavior in the doc.
  2 commentaires
Val Schmidt
Val Schmidt le 7 Mar 2017
Hm. I can't decide if I think that the 0th day of Jan should be interpreted as Dec 31st or not. Part of me thinks that it should generate an error. It is not intuitive that asking for a date in 2017 some number of seconds past midnight should generate a date in the previous year.
Thanks for your help James!
James Tursa
James Tursa le 7 Mar 2017
Modifié(e) : James Tursa le 7 Mar 2017
Well, then you are probably not going to like behavior like this either :)
>> datestr(datenum(2017,1,1,-24,0,0)) % <-- Jan 1st "minus" 24 hours
ans =
31-Dec-2016
But if the functions allow 0's and negative numbers, this is the only behavior that makes sense.
I think all of this is fine ... it is the 0 or 1 for January that is still goofy to me. I would say it is inconsistent with the behavior of datenum for 0's in other positions and might well be considered a bug in my book. Maybe you could post a bug report on this and see what TMW has to say about it. E.g., anything less than 1 for the month seems to be interpreted as a 1 exactly:
>> datestr(datenum(2017,0.5,1,0,0,0))
ans =
01-Jan-2017
>> datestr(datenum(2017,-0.5,1,0,0,0))
ans =
01-Jan-2017
>> datestr(datenum(2017,-1,1,0,0,0))
ans =
01-Jan-2017
>> datestr(datenum(2017,-1.5,1,0,0,0))
ans =
01-Jan-2017
>> datestr(datenum(2017,-1000,1,0,0,0))
ans =
01-Jan-2017

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 7 Mar 2017
January 0th is December 31st. This isn't just a MATLAB convention.
One way to represent January 1st, 2017 as a datetime (which you should use instead of serial date numbers if they're available) is:
dt = datetime(2017, 1, 1, 0, 0, 0)
Another way that refers to the start of the current year:
T = datetime('today');
dt = dateshift(T, 'start', 'year');
To customize the display format to clearly show that dt (as defined by either of those lines of code) refers to midnight on January 1st:
dt.Format = 'dd-MMM-yyyy hh:mm:ss a'

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by