Adding month values to datetime function

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)?

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 26 de Mayo de 2016
Editada: Andrei Bobrov el 26 de Mayo de 2016
out = datetime('now') + calmonths([3,6])
before R2014b
out = addtodate(now ,[3,6],'month')

5 comentarios

Jay
Jay el 27 de Mayo de 2016
Editada: Jay el 27 de Mayo de 2016
Hi Andrei,
I tried using the datetime command before writing this question had an error come up ( I can't remember what the error stated).
All though I have the 2016 version I would like to use the addtodate command as it is more intuitive (for me atleast).
When I have the command
CalWin = addtodate('now', 3, 'month')
I have the following error returned
Date number must be a numeric scalar.
I have used the 'addtodate' help on function ( fx)in the toolstrip.
When I use addtodate(now ,[3,6],'month') I still get the same scalar error.
use now instead of 'now'
CalWin = addtodate(now, 3, 'month')
For Quantity in vector with arrayfun
out = arrayfun(@(x)addtodate(now,x,'month'),[3,6])
Jay
Jay el 27 de Mayo de 2016
Editada: Jay el 27 de Mayo de 2016
Using
CalWin = addtodate(now, 3, 'month')
I get an output of
CalWin = 7.3657e+05
as a double.
Do I need to specify the format or do something else to add 3 months onto the current date?
If I want to use another specified date (ObsDate) other than the current date. The addtodate does not work in its current command form using a values name but only now.
Is it possible to use another value specified with a name rather than now ?
Andrei Bobrov
Andrei Bobrov el 27 de Mayo de 2016
Yes, if your date(ObsDate) - scalar serial date number (read doc about addtodate)
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)

Iniciar sesión para comentar.

Jay
Jay el 27 de Mayo de 2016
Editada: Jay el 27 de Mayo de 2016
Never mind I figured out what was wrong.
CalWin = datenum(ObsDate)
CalWin = addtodate(CalWin, 3, 'month')
datestr(CalWin)

Categorías

Preguntada:

Jay
el 26 de Mayo de 2016

Comentada:

el 27 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by