Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Creating List of Dates, Stepping by a Month

Ever need to create a vector of dates using some sort of pattern? Perhaps these will be used as the edges argument for a histogram, with each a month.

What's the best way to create a datetime array where each element is the first of the month? And what does "best" even mean? - fewest keystrokes, fewest function calls, most readable, most flexible, most maintainable,etc. Suppose I want to produce something like this:

   2018-Jan-01, 2018-Feb-01, 2018-Mar-01, ...

In fact, there are lots of suitable ways. Here are a few.

Contents

Simply Use datetime

Just use datetime and specify the month vectors. This works

mydates1 = datetime(2018,1:12,1)
mydates1 = 
  1×12 datetime array
Columns 1 through 5
   01-Jan-2018   01-Feb-2018   01-Mar-2018   01-Apr-2018   01-May-2018
Columns 6 through 10
   01-Jun-2018   01-Jul-2018   01-Aug-2018   01-Sep-2018   01-Oct-2018
Columns 11 through 12
   01-Nov-2018   01-Dec-2018

While I'm at it, I can find the number of days in each month I have, using eomday.

numDays = eomday(year(mydates1),month(mydates1));
bar(mydates1,numDays)

What if Months Span Years?

If the months in question span years, you can do this fairly compactly using calmonths.

mydates2 = datetime(2017,1,1):calmonths(1):datetime(2018,7,1)
mydates2 = 
  1×19 datetime array
Columns 1 through 5
   01-Jan-2017   01-Feb-2017   01-Mar-2017   01-Apr-2017   01-May-2017
Columns 6 through 10
   01-Jun-2017   01-Jul-2017   01-Aug-2017   01-Sep-2017   01-Oct-2017
Columns 11 through 15
   01-Nov-2017   01-Dec-2017   01-Jan-2018   01-Feb-2018   01-Mar-2018
Columns 16 through 19
   01-Apr-2018   01-May-2018   01-Jun-2018   01-Jul-2018

In addition, you can go beyond 12 months and datetime still works as expected, without needing calmonths.

mydates3 = datetime(2018,1:24,1);

or

mydates4 = datetime(2017,7:18,1)
mydates4 = 
  1×12 datetime array
Columns 1 through 5
   01-Jul-2017   01-Aug-2017   01-Sep-2017   01-Oct-2017   01-Nov-2017
Columns 6 through 10
   01-Dec-2017   01-Jan-2018   01-Feb-2018   01-Mar-2018   01-Apr-2018
Columns 11 through 12
   01-May-2018   01-Jun-2018

However keeping track of the relative month shifts to start has its own mental overhead for me.

Another Way Using calmonths

Try this instead. Find the right start date, and then add on the correct number of calmonths from there.

mydates5 = datetime(2017,7,1) + calmonths(0:11)
mydates5 = 
  1×12 datetime array
Columns 1 through 5
   01-Jul-2017   01-Aug-2017   01-Sep-2017   01-Oct-2017   01-Nov-2017
Columns 6 through 10
   01-Dec-2017   01-Jan-2018   01-Feb-2018   01-Mar-2018   01-Apr-2018
Columns 11 through 12
   01-May-2018   01-Jun-2018

Your Thoughts?

Do you have another way you like to produce lists of dates? What's your preference for these sorts of situations? Let me know here.




Published with MATLAB® R2018b


  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.