Effacer les filtres
Effacer les filtres

search last day calendar in several year

2 vues (au cours des 30 derniers jours)
piero
piero le 21 Sep 2023
Commenté : piero le 22 Sep 2023
yearBegin=2015
yearEnd=2023
endMonth=9;
for anno=yearBegin:yearEnd
for month=1:12
if anno<=yearEnd and month<=endMonth
bb=calendar(anno,mese)
cond=bb(:,7) ;
idx=find(cond);
gg=cond(idx)
hh=[anno,mese,gg,0,0,0]
RP(count)=datetime(hh);
end
end
end
i want to do this:
example:
anno=2015;
endMonth=1;
gg=cond(idx)
cond =
3
10
17
24
31
0
gg =
3
10
17
24
31
RP(1)=3-gen-2015
RP(2)=10-gen-2015
RP(3)=17-gen-2015
RP(4)=24-gen-2015
RP(5)=31-gen-2015
next the second month ( mese=2)
RP(6)=7-feb-2015
RP(7)=14-feb-2015
RP(8)=21-feb-2015
RP(9)=28-feb-2015
...
this loop between 2015-2023 year for every month

Réponse acceptée

dpb
dpb le 21 Sep 2023
Modifié(e) : dpb le 21 Sep 2023
Iffen I interpret the request correctly, you want the Saturdays from 2015 thru 2023...
dt=[datetime(2015,1,1):days(7):datetime(2023,12,31)].'; % datetime from first DOY to last DOY of years by week interval
RP=dateshift(dt,'dayofweek','Saturday'); % shift to following Saturday
RP.Format=['eeee, ' RP.Format]; % format to see DOW string to be sure
[RP(1:5); RP(end-4:end)] % show first, last few for check...
ans = 10×1 datetime array
Saturday, 03-Jan-2015 Saturday, 10-Jan-2015 Saturday, 17-Jan-2015 Saturday, 24-Jan-2015 Saturday, 31-Jan-2015 Saturday, 02-Dec-2023 Saturday, 09-Dec-2023 Saturday, 16-Dec-2023 Saturday, 23-Dec-2023 Saturday, 30-Dec-2023
You don't need to keep the separate datetime array, of course, can build the output array in place; just did for showing...
numel(RP)
ans = 470
shows there are at total of 470 in the array.
  6 commentaires
piero
piero le 22 Sep 2023
thank you
dpb
dpb le 22 Sep 2023
Modifié(e) : dpb le 22 Sep 2023
Changing only to two-week difference won't be reliable; it would produce Saturdays two weeks apart, yes, but not only the first and third for months containing a total of five.
There is not a builtin option to return only the first and third; you'll have to select those by eliminating the ones not in first three weeks of a month or selecting the subset of first, third week in month. I added second answer showing the latter technique.

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 22 Sep 2023
Modifié(e) : dpb le 22 Sep 2023
"...only changing to two-week difference won't be reliable; it would produce Saturdays two weeks apart, yes, but not only the first and third for months containing a total of five."
dt=[datetime(2015,1,1):days(7):datetime(2023,12,31)].'; % datetime from first DOY to last DOY of years by week interval
RP=dateshift(dt,'dayofweek','Saturday'); % shift to following Saturday
RP.Format=['eeee, ' RP.Format]; % format to see DOW string to be sure
WKS=[1,3]; % weeks of month wanted
RP=RP(ismember(week(RP,'weekofmonth'),WKS)); % select the desired subset
[RP(1:5); RP(end-4:end)] % show first, last few for check...
ans = 10×1 datetime array
Saturday, 03-Jan-2015 Saturday, 17-Jan-2015 Saturday, 07-Feb-2015 Saturday, 21-Feb-2015 Saturday, 07-Mar-2015 Saturday, 21-Oct-2023 Saturday, 04-Nov-2023 Saturday, 18-Nov-2023 Saturday, 02-Dec-2023 Saturday, 16-Dec-2023
Adapt to fit; WKS here is a list of weeks of month desired to retain; most any other regular combination could be written with some perturbation of the above type of screening logic.
Clearly you need to peruse the datetime documentation and related links closely; the list of functions at the top of the doc page is invaluable aid in learning of available features...
  9 commentaires
Steven Lord
Steven Lord le 22 Sep 2023
To determine the last day of the month, you could subtract 1 day from the first day of the next month.
dt = datetime(2023, (1:12).' + 1, 1) - caldays(1) % or
dt = 12×1 datetime array
31-Jan-2023 28-Feb-2023 31-Mar-2023 30-Apr-2023 31-May-2023 30-Jun-2023 31-Jul-2023 31-Aug-2023 30-Sep-2023 31-Oct-2023 30-Nov-2023 31-Dec-2023
dt2 = datetime(2023, (1:12).' + 1, 0) % Using the concept of "day 0" of the month
dt2 = 12×1 datetime array
31-Jan-2023 28-Feb-2023 31-Mar-2023 30-Apr-2023 31-May-2023 30-Jun-2023 31-Jul-2023 31-Aug-2023 30-Sep-2023 31-Oct-2023 30-Nov-2023 31-Dec-2023
Or if you have a vector of dates that aren't necessarily the first day of the month, you could dateshift your dates to the end of the month. Take some random data:
data = datetime(2023, randi(12, 5, 1), randi(28, 5, 1))
data = 5×1 datetime array
20-Jan-2023 03-May-2023 22-Apr-2023 19-Jun-2023 09-Mar-2023
and shift the dates to the end of the month.
dt3 = dateshift(data, 'end', 'month')
dt3 = 5×1 datetime array
31-Jan-2023 31-May-2023 30-Apr-2023 30-Jun-2023 31-Mar-2023
piero
piero le 22 Sep 2023
thank

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by