Last Sunday in a month?

1 view (last 30 days)
Andreas Georgakakos
Andreas Georgakakos on 20 Feb 2018
Answered: Peter Perkins on 20 Feb 2018
Hi there,
I am trying to identify the datetime of the last Sunday in March and October. I am aware of the nweekdate function but it requires the nth occurence of the day in that month, as an input. Any ideas would be highly appreciated.
Thank you in advance,
Andreas

Accepted Answer

KSSV
KSSV on 20 Feb 2018
d = calendar(2013,01) ;
s = d(d(:,1)~=0,1)
s(end)

More Answers (1)

Peter Perkins
Peter Perkins on 20 Feb 2018
Unless you are using a version of MATLAB prior to R2014b, use datetimes for dates, not numbers:
>> d = dateshift(datetime(2018,[3 10],1,'Format','eee, dd-MMM-yyyy'),'end','month')
d =
1×2 datetime array
Sat, 31-Mar-2018 Wed, 31-Oct-2018
>> d = dateshift(d,'dayofweek','sunday','previous')
d =
1×2 datetime array
Sun, 25-Mar-2018 Sun, 28-Oct-2018

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!