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?

 采纳的回答

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 个评论

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

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File 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