create yyyymm variable from date

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 comentarios

the cyclist
the cyclist el 30 de Jun. de 2020
Editada: the cyclist el 30 de Jun. de 2020
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 el 30 de Jun. de 2020
Thank you. It is datetime. It is like 1/1/2020. Want to extract just yyyymm out of it.
alpedhuez
alpedhuez el 30 de Jun. de 2020
yyyymmdd=yyyymmdd(Date);
yyyymm=floor(yyyymmdd./100);

Iniciar sesión para comentar.

 Respuesta aceptada

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

3 comentarios

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 el 30 de Jun. de 2020
Thank you. Let me work on it.
Star Strider
Star Strider el 30 de Jun. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 29 de Jun. de 2020

Comentada:

el 30 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by