How to substract months with EOM respect ?

Hi, I would like to know if there is solution to substract months with respect of the End of Month flag. I've tried addtodate() and addMonth() functions but it's not giving me the wanted results.
Here is an exemple : If today's date is 31/01/14 and if I substract 2 months, I'd like as a result the 30/11/2013 and not the 01/12/2013. In the meantime,I like that if today's date is 28/02/2014 and if I substract 2 months, I'd like as a result the 28/12/2013 and not the 31/12/2013 (as addMonth function do).
Many thanks in advance, Regards, Paul

 채택된 답변

Paul
Paul 2014년 1월 23일

0 개 추천

This should works. Logic isn't it ? :-)
function UnadjDate = AddMonthUnadj(date,nbMonth)
Mdate = x2mdate(date); [Y,M,D] = datevec(Mdate);
MatlabDate = addMonth(Mdate,nbMonth,0);
MatlabDateEOM = addMonth(Mdate,nbMonth,1);
if eomdate(Mdate) == Mdate & D ~= [28;29;30]
UnadjDate = m2xdate(MatlabDateEOM);
else
UnadjDate = m2xdate(MatlabDate);
end

추가 답변 (1개)

Mischa Kim
Mischa Kim 2014년 1월 21일

0 개 추천

Hello Paul, there is no function out of the box that does that, as far as I know. You could use the eomday function with a bit of logic. As an alternative you might want to check on File Exchange .

댓글 수: 1

Paul
Paul 2014년 1월 23일
Hi, Logic has always been my weakness :-) but I'll try to find a way.
Thx Mischa

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

카테고리

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

질문:

2014년 1월 21일

답변:

2014년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by