フィルターのクリア

addtodate: unexpected result when adding years

2 ビュー (過去 30 日間)
Andrew
Andrew 2014 年 5 月 19 日
コメント済み: Star Strider 2014 年 5 月 19 日
Hi all,
I'm having an issue with addtodate.m. The example:
addtodate(1, 2014, 'year')
ans =
-734501
is not what I expected... Does anyone know what's going on?
Notes: I'm trying to use addtodate to sanitize a date vector
datevec_unsan = [2014, 1, 1, 40, 0, 0]; % there aren't 40 hours in a day...
types = {'year', 'month', 'day', 'hour', 'minute', 'second'};
tstamp = 0;
for i = 1:6
tstamp = addtodate(tstamp, datevec_unsan(i), types{i});
end
datevec_sanitized = datevec(tstamp);

回答 (2 件)

the cyclist
the cyclist 2014 年 5 月 19 日
編集済み: the cyclist 2014 年 5 月 19 日
I was unaware of this handy function before, but this definitely seems like a bug. The result of
datestr(addtodate(1,2014,'year'))
is '01-Jan-7986', while
datestr(addtodate(1,-2014,'year'))
gives '01-Jan-2014' (which I am guessing is what you expected for your answer originally). Adding just a day or a month seems to work fine, though.
I am about to head out the door, so I can't dig in right now, but you could try
edit addtodate
to poke into the function and see what's going on. I suggest you fill out a bug report, unless someone here points out something obvious that you and I are misunderstanding!
  1 件のコメント
Andrew
Andrew 2014 年 5 月 19 日
Thanks for looking at this. addtodate uses addtodatemx so I don't think we can look at the source sadly

サインインしてコメントする。


Star Strider
Star Strider 2014 年 5 月 19 日
MATLAB will do the correction for you. I don’t know the result you want, but this works:
datevec_unsan = [2014, 1, 1, 40, 0, 0]
x = datenum(datevec_unsan);
datevec_new = datevec(x)
produces:
datevec_unsan =
2014 1 1 40 0 0
datevec_new =
2014 1 2 16 0 0
  2 件のコメント
Andrew
Andrew 2014 年 5 月 19 日
編集済み: Andrew 2014 年 5 月 19 日
Good idea - its even in the datenum documentation that it supports it! And it saves me putting in correction for 1 based days in my original code.
Star Strider
Star Strider 2014 年 5 月 19 日
Thanks!
If it solved your problem, the sincerest form of appreciation her on ‘MATLAB Answers’ is to Accept it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by