Convert serial number dates to month and year

I have a date (736473) that i want to convert to month and year. How do i go about doing that ?

 Akzeptierte Antwort

Paolo
Paolo am 23 Jul. 2018
Bearbeitet: Paolo am 23 Jul. 2018

1 Stimme

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

5 Kommentare

Keturah Palma
Keturah Palma am 23 Jul. 2018
and if i have a huge list of dates. would i still use this?
Paolo
Paolo am 23 Jul. 2018
It would still work, yes. If you want the end result to be a datetime, use my solution. If you would like the end result to be a double, use Walter's solution below.
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)
Keturah Palma
Keturah Palma am 23 Jul. 2018
thank you so much
Paolo
Paolo am 23 Jul. 2018
Bearbeitet: Paolo am 23 Jul. 2018
That's very true Walter, datenum is indeed redundant. I should probably get some sleep... I'll edit.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 23 Jul. 2018

0 Stimmen

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

1 Kommentar

If you want the result as a cell array of character vectors, then you can use
datestr(736473:736475,'yyyy-mm')
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 23 Jul. 2018

Bearbeitet:

am 23 Jul. 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by