Trying to use a for loop, date commands to calculate date of Memorial day for next 10 years
조회 수: 4 (최근 30일)
이전 댓글 표시
I'm trying to use a for loop and the date commands to calculate the date of Memorial day for next 10 years.
cl = clock;
this_year = cl(1)
this_years_MDay = ['25-May-' num2str(this_year)];
[daynum, day_of_the_Mday] = weekday(this_years_MDay, 'long');
day_of_the_Mday = string(day_of_the_Mday);
if floor(now)<datenum(this_years_MDay)
disp('we are before Mday')
years = this_year + [0:9]';
else
disp('we are after Mday')
years = this_year + [1:10]';
end
for n = 1:length(years)
if datetime == week(22)
disp('It could be Mday')
else
datetime == daynum;
disp('It is Mday')
end
end
댓글 수: 0
채택된 답변
Nikita Agrawal
2020년 9월 5일
Use Financial Toolbox from MATLAB and run the following Command.
I am finding the date of Last Monday of May every year to get the date of Memorial Day.
Year = [2020:2029];
LastDate = lweekdate(2, Year, 5);
datestr(LastDate)
This will give you all the required dates. It seems youare using some wrong commands not defining what you call memorial day.
Check out this link : lweekdate , 2 in my answer is for Monday (Sunday is 1) and 5 is for May (January is 1).
Hope this helps. Do Upvote!
댓글 수: 0
추가 답변 (2개)
David Hill
2020년 9월 5일
If you do not have the financial toolbox, here is another method.
t=datetime(2020,5,31);
for k=1:10
t=t+calyears(1);
memorialDay{k}=t-caldays(mod(day(t,'dayofweek')-2,7));
end
댓글 수: 0
Steven Lord
2020년 9월 6일
There's no need for a for loop or Financial Toolbox. Currently Memorial Day is observed on the last Monday in May (though you should special case if your function needs to handle years prior to 1971.) You can compute this with the datetime functionality in MATLAB.
startOfMay = datetime(2019:2022, 5, 1);
endOfMay = dateshift(startOfMay, 'end', 'month');
lastMonday = dateshift(endOfMay, 'dayofweek', 'Monday', 'previous')
Yes, I could have jumped right to defining endOfMay, but I wanted to show how you can shift dates both forward and backward.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!