Adding month values to datetime function

45 次查看(过去 30 天)
If I have a datetime value of today, can I add 3 or 6 months to that value without splitting the date into 3 separate arrays (dd, MM, yyyy)?

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2016-5-26
编辑:Andrei Bobrov 2016-5-26
out = datetime('now') + calmonths([3,6])
before R2014b
out = addtodate(now ,[3,6],'month')
  5 个评论
Andrei Bobrov
Andrei Bobrov 2016-5-27
Yes, if your date(ObsDate) - scalar serial date number (read doc about addtodate)
Andrei Bobrov
Andrei Bobrov 2016-5-27
if ObsDate and Q - array example:
ObsDate = [2016 5 29;2016 6 14];
Q = [3 4 15];
D = datenum(ObsDate);
a = bsxfun(@(x,y)addtodate(x,y,'month'),D(:),Q(:)');
out = arrayfun(@datestr,a,'un',0)

请先登录,再进行评论。


Jay
Jay 2016-5-27
编辑:Jay 2016-5-27
Never mind I figured out what was wrong.
CalWin = datenum(ObsDate)
CalWin = addtodate(CalWin, 3, 'month')
datestr(CalWin)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by