how to have datetime for specific range?

Hi,
I want to have a datetime, monthly from 1982 to 2015, like this:
1/1/1982
2/1/1982
3/1/1982
...
12/31/2015
how to have it?

 Accepted Answer

Try this:
dt1 = datetime([1982 01 01]);
dt2 = datetime([2015 12 31]);
dt_vec = dt1 : calmonths(1) : dt2;
FirstFive = dt_vec(1:5) % Display Results (Delete Later)
producing:
FirstFive =
1×5 datetime array
01-Jan-1982 01-Feb-1982 01-Mar-1982 01-Apr-1982 01-May-1982

3 Comments

oh, thanks greatly appreciated. is there any way to have this in 1/1/1982 format? I mean I don't want to have 1-Jan-1982 but I want 1/1/1982, furthermore, I would like to have all of them in the on column under each other (408*1)
thank you very much
dt1 = datetime([1982 01 01],'format','MM/dd/yyyy');
dt2 = datetime([2015 12 31],'format','MM/dd/yyyy');
dt_vec = dt1 : calmonths(1) : dt2;
FirstFive = dt_vec(1:5)
My pleasure.
Easily done:
dt1 = datetime([1982 01 01]);
dt2 = datetime([2015 12 31]);
dt_vec = (dt1 : calmonths(1) : dt2).';
dt_vec.Format = 'MM/dd/yyyy';
FirstFive = dt_vec(1:5) % Display Results (Delete Later)
producing:
FirstFive =
5×1 datetime array
01/01/1982
02/01/1982
03/01/1982
04/01/1982
05/01/1982

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

BN
on 19 Dec 2019

Commented:

on 19 Dec 2019

Community Treasure Hunt

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

Start Hunting!