search last day calendar in several year
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
dpb
il 21 Set 2023
Modificato: dpb
il 21 Set 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...
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)
shows there are at total of 470 in the array.
6 Commenti
dpb
il 22 Set 2023
Modificato: dpb
il 22 Set 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.
Più risposte (1)
dpb
il 22 Set 2023
Modificato: dpb
il 22 Set 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...
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 Commenti
Steven Lord
il 22 Set 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
dt2 = datetime(2023, (1:12).' + 1, 0) % Using the concept of "day 0" of the month
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))
and shift the dates to the end of the month.
dt3 = dateshift(data, 'end', 'month')
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!