1

I found that functions like day, month and year require Financial Toolbox.

>> [~, packages] = matlab.codetools.requiredFilesAndProducts('month'); packages(2)

ans = 

  struct with fields:

             Name: 'Financial Toolbox'
          Version: '5.8'
    ProductNumber: 30
          Certain: 1

How to know datetime parts without this toolbox?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 2
    This is incorrect; you do not need the Financial Toolbox if you are working with datetimes. The "month", "day", and "year" functions are in base MATLAB, and are methods of the "datetime" class. The "month" you are seeing as the second entry of required files and packages is the Financial Toolbox calendar function, but the datetime methods are themselves in MATLAB. It is generally better to do "which -all month" to figure out what you have and don't have. – CKT Jun 27 '18 at 17:49
  • Here is another proof for you, oh doubting Thomas: `>> which 'month' /usr/local/MATLAB/R2016b/toolbox/finance/calendar/month.m` – Dims Jun 27 '18 at 20:24
  • 2
    You did "which" instead of "which -all". That only shows you the result that is on the top of path, which is for the Financial Toolbox. It does not show you the datetime method, which exists in base MATLAB. You can also see that this method is in MATLAB by looking at its classification in the documentation, where it is listed under MATLAB and not the Financial Toolbox: https://www.mathworks.com/help/matlab/ref/month.html – CKT Jun 27 '18 at 20:35
  • @CKT is right. If you type `methods datetime`, you'll get a full list of methods overloaded for the `datetime` class. All of those methods come with the `datetime` class, and therefore do not require any toolboxes. Additionally, there is a `Month` property: `d=datetime('now'); d.Month`. – Cris Luengo Jun 28 '18 at 04:46

2 Answers2

3

You can use the datestr function (no toolbox required).

For an example, I'll use 'now' to create the datetime object for now, then process:

dt = datetime( 'now' );  % datetime object: 27-Jun-2018 12:17:27

% Year
y = datestr( dt, 'yyyy' ); % >> y = '2018'
% Month
m = datestr( dt, 'mm' );   % >> m = '06'
m = datestr( dt, 'mmm' );  % >> m = 'Jun'
% Day
d = datestr( dt, 'dd' );   % >> d = '27'
d = datestr( dt, 'ddd' );  % >> d = 'Wed'

If you need to convert the numeric year/month/day outputs to numbers from the char arrays, use str2double.


Looking under the hood of, for example, month you can see that (after the robustness checks) it simply indexes an array of month names with the output of datevec, so that would be another way to go.


Edit

I've just seen you can directly access year/month/day/... properties of a datetime object. This makes things pretty easy, especially if you want the numeric output:

dt = datetime( 'now' ); % datetime object: 04-Mar-2019 11:20:00
y = dt.Year;  % >> y = 2019
m = dt.Month; % >> m = 3

This still avoids using the year/month/... functions.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • This is jumping through a lot of hoops for no reason. You can just do: >> dt = datetime('now'); >> y = year(dt); >> m = month(dt); >> d = day(dt); >> [y m d] ans = 2018 6 27 – CKT Jun 27 '18 at 17:53
  • 1
    These functions do not require additional toolboxes. They are in base MATLAB, and are methods of the datetime class. There is no reason to avoid using them in favor of using datestr, which is also in base MATLAB and will be slower. – CKT Jun 28 '18 at 04:05
  • @CKT I now realise you're correct, although your comment on my answer is still pointless... the question asks for this functionality without those functions, I gave a concise (its still just one function) working answer. Your comment on the question itself is more valid – Wolfie Jun 28 '18 at 06:14
  • @Wolfie you are both correct, but since CKT input new knowledge for me, I will move accepted to him :) – Dims Jun 28 '18 at 14:12
3

I don't think that my comments on the question or the accepted solution are getting across in the way I intended, so I'll give a proper answer to better elaborate on what you're seeing.

There are multiple things called "month" in MATLAB. There is a function called month, that is in the Financial Toolbox (https://www.mathworks.com/help/finance/month.html). There is also a method of the datetime class called "month" (https://www.mathworks.com/help/matlab/ref/month.html). These are two separate functions, and act differently. The regular "month" function (of the Financial Toolbox) will accept any date number or string. However, in MATLAB, if you call "month" and pass it an instance of a datetime (as in the accepted solution's answer, you could use datetime('now')), you will not be calling the Financial Toolbox's function "month", but rather the "month" method of the datetime class, because that is how MATLAB dispatching rules work.

matlab.codetools.requiredFilesAndProducts does not and cannot know that you're calling "month" with a datetime input, so it can't assume you're calling the datetime method called "month" and instead reports to you the requirements for the Financial Toolbox function.

If you know you are working with datetimes, you can be more precise in your query to requiredFilesAndProducts:

>> [~,packages] = matlab.codetools.requiredFilesAndProducts(which('month(datetime(''now''))'))

packages = 

         Name: 'MATLAB'
      Version: '8.6'
ProductNumber: 1
      Certain: 1

By using which('month(datetime(''now''))'), you are more precise by telling the which function exactly what your function call is going to look like (i.e., what its input type will be), which allows it to properly figure out which overloaded "month" will get called and then requiredFilesAndProducts can correctly show you that if your input is a datetime, you only need MATLAB -- not the Financial Toolbox.

If you truly need to call "month" on a non-datetime, you can effectively get the same behavior by writing your own function that just puts the serial date and format into a datetime and then calls "month" on the datetime object (see the 'ConvertFrom' syntax on https://www.mathworks.com/help/matlab/ref/datetime.html). This will not have any requirements on the Financial Toolbox, because it will only leverage the datetime method (which is included in base MATLAB).

CKT
  • 781
  • 5
  • 6
  • I don't have the financial toolbox, and `month(datetime('now'))` works. Interesting though, is that `which -all month` returned only the `tall` method until I actually used the `datetime` class, after which point `which -all month` also listed the `datetime` method. Weird! – Cris Luengo Jun 28 '18 at 04:39