detestr/datenum behavior on Jan 1st.
3 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
James Tursa
2017 年 3 月 7 日
編集済み: James Tursa
2017 年 3 月 7 日
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 件のコメント
James Tursa
2017 年 3 月 7 日
編集済み: James Tursa
2017 年 3 月 7 日
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
その他の回答 (1 件)
Steven Lord
2017 年 3 月 7 日
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'
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Time Series Objects についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!