필터 지우기
필터 지우기

Finding the end date of each month/year

조회 수: 5 (최근 30일)
antonet
antonet 2012년 6월 25일
Dear all,
I have the following sequence of dates
dates =[ '23/11/08'
'28/12/08'
'25/01/09'
'22/02/09'
'29/03/09'
'26/04/09'
'24/05/09'
'28/06/09'
'26/07/09'];
Is there any way to find the end date for each of these months.
For example the last date of November 2008 (11/08) is 30/11/08. Similarly, the last date of December 2008 is 31/12/08 and so forth. I have a large vector of “dates” and an “quick” way to find these dates would be better
Thank you

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 6월 25일
one way
dates =[ '23/11/08'
'28/12/08'
'25/01/09'
'22/02/09'
'29/03/09'
'26/04/09'
'24/05/09'
'28/06/09'
'26/07/09'];
dv = datevec(dates,'dd/mm/yy');
dc = num2cell(dv(:,1:2),1);
enddates = datestr(datenum(dc{:},eomday(dc{:})),'dd/mm/yy');
or
[Y, M] = datevec(dates,'dd/mm/yy');
out = datestr(datenum([Y, M, eomday(Y, M)]),'dd/mm/yy');
second way with use function eomdate from Financial Toolbox
enddates = datestr(eomdate(datenum(dates,'dd/mm/yy')),'dd/mm/yy');
other way
[Y, M] = datevec(dates,'dd/mm/yy');
enddates = datestr(datenum(Y,M+1,1)-1,'dd/mm/yy');

추가 답변 (1개)

grapevine
grapevine 2012년 6월 25일
This is what you are looking
E = eomday(Y, M)
returns the last day of the year and month given by corresponding elements of arrays Y and M.
  댓글 수: 1
antonet
antonet 2012년 6월 25일
Thank you! i did not know that such function exists
So this means that I have to do something like
eomday(2008, 11:12)
and eomday(2009, 1:7) which is fine.
But this approach seems to be "quite manual".
My goal is to replace the above "dates" vector with the "end dates" vector less
"manually"
That is, to replace
dates =[ '23/11/08'
'28/12/08'
'25/01/09'
'22/02/09'
''29/03/09'
' '26/04/09'
'24/05/09' '
'28/06/09' '
'26/07/09'];
with
enddates=[31/11/08
30/12/08
.
.
.]
Is there a way to do that more quickly?
thank you again

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by