{"id":3242,"date":"2019-03-06T10:44:10","date_gmt":"2019-03-06T15:44:10","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3242"},"modified":"2019-02-19T10:45:33","modified_gmt":"2019-02-19T15:45:33","slug":"creating-list-of-dates-stepping-by-a-month","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2019\/03\/06\/creating-list-of-dates-stepping-by-a-month\/","title":{"rendered":"Creating List of Dates, Stepping by a Month"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>Ever need to create a vector of dates using some sort of pattern? Perhaps these will be used as the <tt>edges<\/tt> argument for a histogram, with each a month.<\/p><p>What's the best way to create a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html\"><tt>datetime<\/tt><\/a> 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:<\/p><pre>   2018-Jan-01, 2018-Feb-01, 2018-Mar-01, ...<\/pre><p>In fact, there are lots of suitable ways.  Here are a few.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#fa786c3a-b8f9-4bb6-bcb5-2b97968c55a8\">Simply Use datetime<\/a><\/li><li><a href=\"#41ff0368-040c-4d24-8f97-1ff99e847fe4\">What if Months Span Years?<\/a><\/li><li><a href=\"#a3fad390-5599-4082-9365-ed84126e3a92\">Another Way Using calmonths<\/a><\/li><li><a href=\"#9a711bda-bd0f-4373-be58-f14ba9442d51\">Your Thoughts?<\/a><\/li><\/ul><\/div><h4>Simply Use datetime<a name=\"fa786c3a-b8f9-4bb6-bcb5-2b97968c55a8\"><\/a><\/h4><p>Just use <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html\">datetime<\/a><\/tt> and specify the month vectors. This works<\/p><pre class=\"codeinput\">mydates1 = datetime(2018,1:12,1)\r\n<\/pre><pre class=\"codeoutput\">mydates1 = \r\n  1&times;12 datetime array\r\nColumns 1 through 5\r\n   01-Jan-2018   01-Feb-2018   01-Mar-2018   01-Apr-2018   01-May-2018\r\nColumns 6 through 10\r\n   01-Jun-2018   01-Jul-2018   01-Aug-2018   01-Sep-2018   01-Oct-2018\r\nColumns 11 through 12\r\n   01-Nov-2018   01-Dec-2018\r\n<\/pre><p>While I'm at it, I can find the number of days in each month I have, using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/eomday.html\"><tt>eomday<\/tt><\/a>.<\/p><pre class=\"codeinput\">numDays = eomday(year(mydates1),month(mydates1));\r\nbar(mydates1,numDays)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/images\/loren\/2019\/saveTheDate_01.png\" alt=\"\"> <h4>What if Months Span Years?<a name=\"41ff0368-040c-4d24-8f97-1ff99e847fe4\"><\/a><\/h4><p>If the months in question span years,  you can do this fairly compactly using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/calmonths.html\"><tt>calmonths<\/tt><\/a>.<\/p><pre class=\"codeinput\">mydates2 = datetime(2017,1,1):calmonths(1):datetime(2018,7,1)\r\n<\/pre><pre class=\"codeoutput\">mydates2 = \r\n  1&times;19 datetime array\r\nColumns 1 through 5\r\n   01-Jan-2017   01-Feb-2017   01-Mar-2017   01-Apr-2017   01-May-2017\r\nColumns 6 through 10\r\n   01-Jun-2017   01-Jul-2017   01-Aug-2017   01-Sep-2017   01-Oct-2017\r\nColumns 11 through 15\r\n   01-Nov-2017   01-Dec-2017   01-Jan-2018   01-Feb-2018   01-Mar-2018\r\nColumns 16 through 19\r\n   01-Apr-2018   01-May-2018   01-Jun-2018   01-Jul-2018\r\n<\/pre><p>In addition, you can go beyond 12 months and <tt>datetime<\/tt> still works as expected, without needing <tt>calmonths<\/tt>.<\/p><pre class=\"codeinput\">mydates3 = datetime(2018,1:24,1);\r\n<\/pre><p>or<\/p><pre class=\"codeinput\">mydates4 = datetime(2017,7:18,1)\r\n<\/pre><pre class=\"codeoutput\">mydates4 = \r\n  1&times;12 datetime array\r\nColumns 1 through 5\r\n   01-Jul-2017   01-Aug-2017   01-Sep-2017   01-Oct-2017   01-Nov-2017\r\nColumns 6 through 10\r\n   01-Dec-2017   01-Jan-2018   01-Feb-2018   01-Mar-2018   01-Apr-2018\r\nColumns 11 through 12\r\n   01-May-2018   01-Jun-2018\r\n<\/pre><p>However keeping track of the relative month shifts to start has its own mental overhead for me.<\/p><h4>Another Way Using calmonths<a name=\"a3fad390-5599-4082-9365-ed84126e3a92\"><\/a><\/h4><p>Try this instead. Find the right start date, and then add on the correct number of <tt>calmonths<\/tt> from there.<\/p><pre class=\"codeinput\">mydates5 = datetime(2017,7,1) + calmonths(0:11)\r\n<\/pre><pre class=\"codeoutput\">mydates5 = \r\n  1&times;12 datetime array\r\nColumns 1 through 5\r\n   01-Jul-2017   01-Aug-2017   01-Sep-2017   01-Oct-2017   01-Nov-2017\r\nColumns 6 through 10\r\n   01-Dec-2017   01-Jan-2018   01-Feb-2018   01-Mar-2018   01-Apr-2018\r\nColumns 11 through 12\r\n   01-May-2018   01-Jun-2018\r\n<\/pre><h4>Your Thoughts?<a name=\"9a711bda-bd0f-4373-be58-f14ba9442d51\"><\/a><\/h4><p>Do you have another way you like to produce lists of dates?  What's your preference for these sorts of situations?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3242#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_f912ee1787424e3780c5831f6bc11d2e() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='f912ee1787424e3780c5831f6bc11d2e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f912ee1787424e3780c5831f6bc11d2e';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f912ee1787424e3780c5831f6bc11d2e()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><\/div><!--\r\nf912ee1787424e3780c5831f6bc11d2e ##### SOURCE BEGIN #####\r\n%% Creating List of Dates, Stepping by a Month\r\n% Ever need to create a vector of dates using some sort of pattern?  \r\n% Perhaps these will be used as the |edges| argument for \r\n% a histogram, with each a month.\r\n%\r\n% What's the best way to create a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html |datetime|>\r\n% array where each element is the first of the month? And what does \"best\"\r\n% even mean? - fewest keystrokes, fewest function calls, most readable,\r\n% most flexible, most maintainable,etc. Suppose I want to produce something\r\n% like this:\r\n% \r\n%     2018-Jan-01, 2018-Feb-01, 2018-Mar-01, ...\r\n% \r\n% In fact, there are lots of suitable ways.  Here are a few.\r\n%\r\n%% Simply Use datetime\r\n% Just use |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html datetime>| and specify the month vectors.\r\n% This works\r\n%\r\nmydates1 = datetime(2018,1:12,1)\r\n%%\r\n% While I'm at it, I can find the number of days in each month I have,\r\n% using <https:\/\/www.mathworks.com\/help\/matlab\/ref\/eomday.html\r\n% |eomday|>.\r\nnumDays = eomday(year(mydates1),month(mydates1));\r\nbar(mydates1,numDays)\r\n%% What if Months Span Years?\r\n% If the months in question span years,  you can do this fairly compactly using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/calmonths.html |calmonths|>.\r\n%\r\nmydates2 = datetime(2017,1,1):calmonths(1):datetime(2018,7,1)\r\n%%\r\n% In addition, you can go beyond 12 months and |datetime| still works\r\n% as expected, without needing |calmonths|. \r\n%\r\nmydates3 = datetime(2018,1:24,1);\r\n%%\r\n% or\r\nmydates4 = datetime(2017,7:18,1)\r\n%%\r\n% However keeping track of the relative month shifts to start has its own\r\n% mental overhead for me.\r\n%% Another Way Using calmonths\r\n% Try this instead. Find the right start date, and then add on the correct\r\n% number of |calmonths| from there.\r\nmydates5 = datetime(2017,7,1) + calmonths(0:11)\r\n \r\n%% Your Thoughts?\r\n% Do you have another way you like to produce lists of dates?  What's your\r\n% preference for these sorts of situations?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=3242#respond here>.\r\n\r\n##### SOURCE END ##### f912ee1787424e3780c5831f6bc11d2e\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"http:\/\/blogs.mathworks.com\/images\/loren\/2019\/saveTheDate_01.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Ever need to create a vector of dates using some sort of pattern? Perhaps these will be used as the <tt>edges<\/tt> argument for a histogram, with each a month.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2019\/03\/06\/creating-list-of-dates-stepping-by-a-month\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[57],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3242"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3242"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3242\/revisions"}],"predecessor-version":[{"id":3246,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3242\/revisions\/3246"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}