# Simple Date Manipulations

I've recently had an opportunity to work with data for which I wanted to exclude weekend dates in order to see some trends. It took a little getting used to for me, because absolute time is not germane to the scientific and engineering data I am accustomed to.

### Excel Data

I was given some data extracted from an Excel spreadsheet. Here's some of it.

mytimes = [40314.338356482
40315.334988426
40316.333935185
40316.333935237
40317.341400463
40318.336875000
40318.336875032
40318.336875295
40319.333912037
40320.337835648
40321.335960648
40322.342372685
40323.342106481
40324.335439815
40325.337696759
40326.338171033
40326.338171296
40327.340648148
40328.338912037
40329.336631944
40330.338657407
40331.337256944
40332.342314815
40333.342164352
40334.334004630
40335.341342593
40336.340104167
40338.334884259
40339.337777094
40339.337777778
40340.333842593];

As you can see, the times have a basis that is not this year! Digging around, I find I can convert these to datenum by adding a magic number. This is a conversion that you can get using the function x2mdate from Financial Toolbox.

 newtimes = mytimes + 693960;

To see if this makes sense, let me extract a date string. Looking at the documentation, you can see many options for formatting the date. I have used the default setting.

 lastday = datestr(newtimes(end))
lastday =
11-Jun-2010 08:00:44


I want to remove weekends from the data and only care about date, not time of day. I also want to remove duplicate dates. Let me first get the dates alone and create a unique list.

 daysonly = fix(mytimes)
daysonly =
40314
40315
40316
40316
40317
40318
40318
40318
40319
40320
40321
40322
40323
40324
40325
40326
40326
40327
40328
40329
40330
40331
40332
40333
40334
40335
40336
40338
40339
40339
40340

 uniquedays = unique(daysonly)
uniquedays =
40314
40315
40316
40317
40318
40319
40320
40321
40322
40323
40324
40325
40326
40327
40328
40329
40330
40331
40332
40333
40334
40335
40336
40338
40339
40340


Using another date function, I can find the days in my data corresponding to Saturday and Sunday, weekend days in the US.

n = weekday(uniquedays);

Find Saturday and Sunday.

weekends = find(n==1 | n==7);
% Remove weekend days.
myweekdays = uniquedays;
myweekdays(weekends) = [];

You can see I've deleted 7 dates from my unique list, corresponding to the weekend days.

whos
  Name             Size            Bytes  Class     Attributes

daysonly        31x1               248  double
lastday          1x20               40  char
mytimes         31x1               248  double
myweekdays      18x1               144  double
n               26x1               208  double
newtimes        31x1               248  double
s               18x3               108  char
uniquedays      26x1               208  double
weekends         8x1                64  double



I can check that the days I have left are weekdays.

[n,s] = weekday(myweekdays,'short');
s
s =
Mon
Tue
Wed
Thu
Fri
Mon
Tue
Wed
Thu
Fri
Mon
Tue
Wed
Thu
Fri
Tue
Wed
Thu


### More Date Tools

Another date tool you might find useful is the function datetick, for labeling plots. For more on dates in MATLAB, check out the documentation.

I am curious how many people have data, other than financial data, where the actual date, rather than a relative one, is important to your data analysis. Let me know here.

Published with MATLAB® 7.10

|

### Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.