How to define sequence of dates between to dates on monthly time step?

13 views (last 30 days)
Hello, I want to generate sequence of dates between two dates on monthly time step, however, couldnt figure a way out. I have tried various options such as datenum, datestr, datetime etc but could not solve my problem. Here is a sample of one trial.
StartDate= datetime(1981,01,01);
EndDate=datetime(2010,12,31);
formatOut='yyyy-mm';
Date=StartDate:EndDate;
Date=datetime(Date,formatOut);
I know it won't work as the number of the argument are wrong. but I want the date in the following format. Any help would be highly appreciated.
Jan-1981
Feb-1981
Mar-1981
-------
--------
Dec- 2010

Accepted Answer

Peter Perkins
Peter Perkins on 19 Dec 2017
Hydro, I think all you need is to add a step size of one calendar month (in your code you are stepping by one day):
StartDate = datetime(1981,01,01);
EndDate = datetime(2010,12,31);
Date = StartDate:calmonths(1):EndDate;
Date.Format = 'MMM-yyyy'

More Answers (1)

James Knowles
James Knowles on 18 Dec 2017
Here's a similar function I wrote for a similar purpose. It should put you on the write track and allow you to manipulate it for your own purposes.
function[m,y] = month_year(idx2,idx3)
month = ["Jan", "Feb", "March", "April", "May", "June", "July","Aug", "Sept", "Oct", "Nov", "Dec"]; year = [1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012];
m = month(idx2); y = year(idx3);
end

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!