help writing a script that tells the day of the week

3 visualizaciones (últimos 30 días)
bachir
bachir el 9 de Oct. de 2015
Comentada: Star Strider el 10 de Oct. de 2015
I need help writing a code that tells me the day of the week as well as the number of the day relative to the year. Ex: input form month/day/year: xx/xx/xxxx output should say something like: The date 08/31/2011 is the 243 day of the year and is a wednesday.
I have tried so many different things from Mathworks with no luck.
  2 comentarios
John D'Errico
John D'Errico el 10 de Oct. de 2015
disp('Today is Monday")
Correct at least once a week. Oh. did you want it to get the day right, all the time?
bachir
bachir el 10 de Oct. de 2015
I've got it from Star Strider

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 10 de Oct. de 2015
One way (not using datetime, since not everyone may have it):
InpStr = '08/31/2011';
YrStart = datenum(InpStr(end-3:end), 'yyyy');
dtnm = datenum(InpStr, 'mm/dd/yyyy');
[DayOfWkNum,DayOfWkStr] = weekday(dtnm,'long');
fprintf(1, '\n\tThe date %s is the %d day of the year and is a %s\n', InpStr, dtnm-YrStart+1, DayOfWkStr)
The date 08/31/2011 is the 243 day of the year and is a Wednesday
  2 comentarios
bachir
bachir el 10 de Oct. de 2015
Star Strider, You the man! or woman...! I modified it a little to fit my style but thanks a lot
Star Strider
Star Strider el 10 de Oct. de 2015
My pleasure, and quite definitely male!
Have fun!

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 10 de Oct. de 2015
... and another way, using datetime (if you have R2014b or later):
>> d = datetime('08/31/2011','InputFormat','MM/dd/yyyy')
d =
31-Aug-2011
>> day(d,'dayofyear')
ans =
243
>> day(d,'name')
ans =
'Wednesday'

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by