{"id":235,"date":"2010-06-11T13:54:06","date_gmt":"2010-06-11T13:54:06","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/06\/11\/simple-date-manipulations\/"},"modified":"2010-06-11T13:59:51","modified_gmt":"2010-06-11T13:59:51","slug":"simple-date-manipulations","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/06\/11\/simple-date-manipulations\/","title":{"rendered":"Simple Date Manipulations"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I've recently had an opportunity to work with data for which I wanted to exclude weekend dates in order to see some trends.\r\n         It took a little getting used to for me, because absolute time is not germane to the scientific and engineering data I am\r\n         accustomed to.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Excel Data<\/a><\/li>\r\n         <li><a href=\"#10\">More Date Tools<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Excel Data<a name=\"1\"><\/a><\/h3>\r\n   <p>I was given some data extracted from an Excel spreadsheet.  Here's some of it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mytimes = [40314.338356482\r\n           40315.334988426\r\n           40316.333935185\r\n           40316.333935237\r\n           40317.341400463\r\n           40318.336875000\r\n           40318.336875032\r\n           40318.336875295\r\n           40319.333912037\r\n           40320.337835648\r\n           40321.335960648\r\n           40322.342372685\r\n           40323.342106481\r\n           40324.335439815\r\n           40325.337696759\r\n           40326.338171033\r\n           40326.338171296\r\n           40327.340648148\r\n           40328.338912037\r\n           40329.336631944\r\n           40330.338657407\r\n           40331.337256944\r\n           40332.342314815\r\n           40333.342164352\r\n           40334.334004630\r\n           40335.341342593\r\n           40336.340104167\r\n           40338.334884259\r\n           40339.337777094\r\n           40339.337777778\r\n           40340.333842593];<\/pre><p>As you can see, the times have a basis that is not this year!  Digging around, I find I can convert these to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/datenum.html\"><tt>datenum<\/tt><\/a> by adding a magic number.  This is a conversion that you can get using the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/finance\/x2mdate.html\"><tt>x2mdate<\/tt><\/a> from <a href=\"https:\/\/www.mathworks.com\/products\/finance\/\">Financial Toolbox<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"> newtimes = mytimes + 693960;<\/pre><p>To see if this makes sense, let me extract a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/finance\/datestr.html\">date string<\/a>.  Looking at the documentation, you can see many options for formatting the date.  I have used the default setting.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"> lastday = datestr(newtimes(end))<\/pre><pre style=\"font-style:oblique\">lastday =\r\n11-Jun-2010 08:00:44\r\n<\/pre><p>I want to remove weekends from the data and only care about date, not time of day. I also want to remove duplicate dates.\r\n       Let me first get the dates alone and create a unique list.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"> daysonly = fix(mytimes)<\/pre><pre style=\"font-style:oblique\">daysonly =\r\n       40314\r\n       40315\r\n       40316\r\n       40316\r\n       40317\r\n       40318\r\n       40318\r\n       40318\r\n       40319\r\n       40320\r\n       40321\r\n       40322\r\n       40323\r\n       40324\r\n       40325\r\n       40326\r\n       40326\r\n       40327\r\n       40328\r\n       40329\r\n       40330\r\n       40331\r\n       40332\r\n       40333\r\n       40334\r\n       40335\r\n       40336\r\n       40338\r\n       40339\r\n       40339\r\n       40340\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"> uniquedays = unique(daysonly)<\/pre><pre style=\"font-style:oblique\">uniquedays =\r\n       40314\r\n       40315\r\n       40316\r\n       40317\r\n       40318\r\n       40319\r\n       40320\r\n       40321\r\n       40322\r\n       40323\r\n       40324\r\n       40325\r\n       40326\r\n       40327\r\n       40328\r\n       40329\r\n       40330\r\n       40331\r\n       40332\r\n       40333\r\n       40334\r\n       40335\r\n       40336\r\n       40338\r\n       40339\r\n       40340\r\n<\/pre><p>Using another date function, I can find the days in my data corresponding to Saturday and Sunday, weekend days in the US.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">n = weekday(uniquedays);<\/pre><p>Find Saturday and Sunday.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">weekends = find(n==1 | n==7);\r\n<span style=\"color: #228B22\">% Remove weekend days.<\/span>\r\nmyweekdays = uniquedays;\r\nmyweekdays(weekends) = [];<\/pre><p>You can see I've deleted 7 dates from my unique list, corresponding to the weekend days.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos<\/pre><pre style=\"font-style:oblique\">  Name             Size            Bytes  Class     Attributes\r\n\r\n  daysonly        31x1               248  double              \r\n  lastday          1x20               40  char                \r\n  mytimes         31x1               248  double              \r\n  myweekdays      18x1               144  double              \r\n  n               26x1               208  double              \r\n  newtimes        31x1               248  double              \r\n  s               18x3               108  char                \r\n  uniquedays      26x1               208  double              \r\n  weekends         8x1                64  double              \r\n\r\n<\/pre><p>I can check that the days I have left are weekdays.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[n,s] = weekday(myweekdays,<span style=\"color: #A020F0\">'short'<\/span>);\r\ns<\/pre><pre style=\"font-style:oblique\">s =\r\nMon\r\nTue\r\nWed\r\nThu\r\nFri\r\nMon\r\nTue\r\nWed\r\nThu\r\nFri\r\nMon\r\nTue\r\nWed\r\nThu\r\nFri\r\nTue\r\nWed\r\nThu\r\n<\/pre><h3>More Date Tools<a name=\"10\"><\/a><\/h3>\r\n   <p>Another date tool you might find useful is the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/datetick.html\"><tt>datetick<\/tt><\/a>, for labeling plots.  For more on dates in MATLAB, check out the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/f16-42340.html#f16-32430\">documentation<\/a>.\r\n   <\/p>\r\n   <p>I am curious how many people have data, other than financial data, where the actual date, rather than a relative one, is important\r\n      to your data analysis.  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=235#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_d2aab9a339484e1ca54924476eef85fe() {\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='d2aab9a339484e1ca54924476eef85fe ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d2aab9a339484e1ca54924476eef85fe';\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        author = 'Loren Shure';\r\n        copyright = 'Copyright 2010 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 author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\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      \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_d2aab9a339484e1ca54924476eef85fe()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\nd2aab9a339484e1ca54924476eef85fe ##### SOURCE BEGIN #####\r\n%% Simple Date Manipulations\r\n% I've recently had an opportunity to work with data for which I wanted to\r\n% exclude weekend dates in order to see some trends.  It took a little\r\n% getting used to for me, because absolute time is not germane to the\r\n% scientific and engineering data I am accustomed to.\r\n%% Excel Data\r\n% I was given some data extracted from an Excel spreadsheet.  Here's some\r\n% of it.\r\nmytimes = [40314.338356482\r\n           40315.334988426\r\n           40316.333935185\r\n           40316.333935237\r\n           40317.341400463\r\n           40318.336875000\r\n           40318.336875032\r\n           40318.336875295\r\n           40319.333912037\r\n           40320.337835648\r\n           40321.335960648\r\n           40322.342372685\r\n           40323.342106481\r\n           40324.335439815\r\n           40325.337696759\r\n           40326.338171033\r\n           40326.338171296\r\n           40327.340648148\r\n           40328.338912037\r\n           40329.336631944\r\n           40330.338657407\r\n           40331.337256944\r\n           40332.342314815\r\n           40333.342164352\r\n           40334.334004630\r\n           40335.341342593\r\n           40336.340104167\r\n           40338.334884259\r\n           40339.337777094\r\n           40339.337777778\r\n           40340.333842593];\r\n %%\r\n % As you can see, the times have a basis that is not this year!  Digging\r\n % around, I find I can convert these to\r\n % <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/datenum.html\r\n % |datenum|> by adding a magic number.  This is a conversion that you can\r\n % get using the function\r\n % <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/finance\/x2mdate.html |x2mdate|>\r\n % from <https:\/\/www.mathworks.com\/products\/finance\/ Financial Toolbox>.\r\n newtimes = mytimes + 693960;\r\n %%\r\n % To see if this makes sense, let me extract a \r\n % <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/finance\/datestr.html \r\n % date string>.  Looking at the documentation, you can see many options\r\n % for formatting the date.  I have used the default setting.\r\n lastday = datestr(newtimes(end))\r\n %%\r\n % I want to remove weekends from the data and only care about date, not\r\n % time of day. I also want to remove duplicate dates.  Let me first get\r\n % the dates alone and create a unique list.\r\n daysonly = fix(mytimes)\r\n %%\r\n uniquedays = unique(daysonly)\r\n \r\n%%\r\n% Using another date function, I can find the days in my data\r\n% corresponding to Saturday and Sunday, weekend days in the US.\r\nn = weekday(uniquedays);\r\n%%\r\n% Find Saturday and Sunday.\r\nweekends = find(n==1 | n==7); \r\n% Remove weekend days.\r\nmyweekdays = uniquedays;\r\nmyweekdays(weekends) = [];\r\n%%\r\n% You can see I've deleted 7 dates from my unique list, corresponding to\r\n% the weekend days.\r\nwhos\r\n%%\r\n% I can check that the days I have left are weekdays.\r\n[n,s] = weekday(myweekdays,'short');\r\ns\r\n%% More Date Tools\r\n% Another date tool you might find useful is the function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/datetick.html\r\n% |datetick|>, for labeling plots.  For more on dates in MATLAB, check out\r\n% the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/f16-42340.html#f16-32430\r\n% documentation>.\r\n%%\r\n% I am curious how many people have data, other than financial data, where\r\n% the actual date, rather than a relative one, is important to your data\r\n% analysis.  Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=235#respond\r\n% here>.\r\n\r\n \r\n \r\n##### SOURCE END ##### d2aab9a339484e1ca54924476eef85fe\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I've recently had an opportunity to work with data for which I wanted to exclude weekend dates in order to see some trends.\r\n         It took a little getting used to for me,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/06\/11\/simple-date-manipulations\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/235"}],"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=235"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}