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 ?

 채택된 답변

Paolo
Paolo 2018년 7월 23일
편집: Paolo 2018년 7월 23일

1 개 추천

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

댓글 수: 5

and if i have a huge list of dates. would i still use this?
Paolo
Paolo 2018년 7월 23일
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)
thank you so much
Paolo
Paolo 2018년 7월 23일
편집: Paolo 2018년 7월 23일
That's very true Walter, datenum is indeed redundant. I should probably get some sleep... I'll edit.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 7월 23일

0 개 추천

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

댓글 수: 1

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

질문:

2018년 7월 23일

편집:

2018년 7월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by