MATLAB Answers


Convert serial number dates to month and year

Asked by Keturah Palma on 23 Jul 2018
Latest activity Edited by Paolo
on 23 Jul 2018
I have a date (736473) that i want to convert to month and year. How do i go about doing that ?


Sign in to comment.


2 Answers

Answer by Paolo
on 23 Jul 2018
Edited by Paolo
on 23 Jul 2018
 Accepted Answer

mydate = datetime(736473,'ConvertFrom','datenum','Format','yyyy-MM')


In the above code, the call to datenum() is not needed: the value such as 736473 is already a serial date number.
You can pass a vector or array of values in the var variable in the above code.
Note that the result of the above will be a series of datetime objects, not character vectors. If you wanted the text as the result, you could cellstr(mydate)
thank you so much
That's very true Walter, datenum is indeed redundant. I should probably get some sleep... I'll edit.

Sign in to comment.

Answer by Walter Roberson
on 23 Jul 2018

dv = datevec(736473);
year = dv(:,1);
month = dv(:,2);

  1 Comment

If you want the result as a cell array of character vectors, then you can use
for example.
Note that for datestr(), the coding for month is lower-case m, but that for the newer datetime(), the coding for month is upper-case M.

Sign in to comment.