I have a date variable. I want to create yyyymm from the date variable. I can use the year function and the month function to extract year and month. I would like to know how one can create yyyymm variable.

댓글 수: 3

the cyclist
the cyclist 2020년 6월 30일
편집: the cyclist 2020년 6월 30일
When you say you have a "date variable", what format is it? Is it a datenumber? A datetime? Something else? Can you post an example of your data, either by posting some code, or uploading in a MAT file?
alpedhuez
alpedhuez 2020년 6월 30일
Thank you. It is datetime. It is like 1/1/2020. Want to extract just yyyymm out of it.
yyyymmdd=yyyymmdd(Date);
yyyymm=floor(yyyymmdd./100);

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

 채택된 답변

Star Strider
Star Strider 2020년 6월 30일

0 개 추천

Try this:
dt = '1/1/2020';
Out = datetime(dt, 'InputFormat','MM/dd/yyyy', 'Format','yyyyMM')
producing:
Out =
datetime
202001
.

댓글 수: 3

I should also have asked OP what format they wanted for the output, too. :-)
It's best to leave things as datetime format where possible, if you need to convert to string or character, then just add
string(Out)
or
char(Out)
to this solution.
alpedhuez
alpedhuez 2020년 6월 30일
Thank you. Let me work on it.
Star Strider
Star Strider 2020년 6월 30일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2020a

태그

질문:

2020년 6월 29일

댓글:

2020년 6월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by