-
Notifications
You must be signed in to change notification settings - Fork 287
/
dayofyear.m
54 lines (43 loc) · 1.27 KB
/
dayofyear.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function doy = dayofyear(mmddyy,dateFormat)
%DAYOFYEAR Converts a date string ("mm/dd/yyyy") to the day number of the
%year.
% NOTE: MATLAB already does easily this using:
% doy = day(d,"dayofyear")
% where d is a datetime object
% Copyright 2022 The MathWorks, Inc.
arguments
mmddyy string;
dateFormat (1,1) string {mustBeMember(dateFormat,["mm/dd/yyyy","dd/mm/yyyy"])} = "mm/dd/yyyy";
end
% Check that mmddyy was provided in the appropriate format
if numel(split(mmddyy,"/")) ~= 3
error("dayofyear:InvalidDateFormat","Invalid date string. Expected date formatted as dd/mm/yyyy.")
end
% Create a datetime object depending on the dateFormat provided
if dateFormat == "mm/dd/yyyy"
d = datetime(mmddyy,"Format","MM/dd/uuuu");
else
d = datetime(mmddyy,"Format","dd/MM/uuuu");
end
% Initialize the days per month
daysPerMonth = [ ...
31; % January
28; % February
31; % March
30; % April
31; % May
30; % June
31; % July
31; % August
30; % September
31; % October
30; % November
31]; % December
% Check for leap year
if mod(d.Year,4) == 0
% This is a leap year, so change February to 29 days
daysPerMonth(2) = 29;
end
% Calculate day of year
doy = sum(daysPerMonth(1:d.Month-1)) + d.Day;
end