# Converting a date string to day of year

147 Ansichten (letzte 30 Tage)
NewbieCA am 21 Feb. 2011
Kommentiert: Peter Perkins am 2 Mär. 2022
Hi there I have a simple question that I can't figure out an answer to. I have a column of dates in mm/dd/yyyy format and I need to convert the dates to a Day of Year (1 to 365/366) and then export a file that has the year in column A and the Day of Year in Column B. The data set starts on Jan 1st 1957 so obviously includes leap years. Any advice would be greatly appreciated. Thanks.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jan am 22 Feb. 2011
Assuming that your input data is a cell string:
DC = {'01/02/2010'; '02/02/2010'};
DV = datevec(DC); % [N x 6] array
DV = DV(:, 1:3); % [N x 3] array, no time
DV2 = DV;
DV2(:, 2:3) = 0; % [N x 3], day before 01.Jan
Result = cat(2, DV(:, 1), datenum(DV) - datenum(DV2));
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Siyavuya Madlanga am 6 Dez. 2017
Thank you very much
Peter Perkins am 19 Dez. 2017
Since R2014b, using datetime is a much better choice:
>> d = datetime('now')
d =
datetime
18-Dec-2017 22:20:02
>> doy = day(d,'dayofyear')
doy =
352

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (3)

James Tursa am 22 Feb. 2011
Here is an outline of code to get Day Of Year:
>> d = '12/25/1957'
d =
12/25/1957
>> v = datevec(d)
v =
1957 12 25 0 0 0
>> v0 = v
v0 =
1957 12 25 0 0 0
>> v0(:,2:3) = 1
v0 =
1957 1 1 0 0 0
>> datenum(v) - datenum(v0) + 1
ans =
359
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
NewbieCA am 22 Feb. 2011
Thanks for this

Melden Sie sich an, um zu kommentieren.

Ian am 27 Jul. 2015
Bearbeitet: Ian am 27 Jul. 2015
As Tursa's example implies, matlab datenum's are numeric timestamps (of type double) that can be subtracted (to get a relative offset) or added to. They are simply time elapsed (in days, & can be fractional) since Jan. 0, year 0000. (See 'doc datenum').
A slightly more complete/compact version of Tursa's answer:
d = datevec('12/25/1957');
v = datenum(d); % v now == [1957 12 25];
day = v - datenum(d(1), 1,0); % datenum(yr,1,0) == datenum(yr-1,12,31)
Datenum( ... ) will operate on arrays of dates or datevecs, so a little creative programming can extract the day-of-year for an array of dates given as strings.
Matlab's double's are (currently) ieee 754 format, so the precision is about 1e-10 days, or ballpark 10 usecs, as the following example shows:
i
>> d1=datenum(now);
>> d2=[d1+1e-10, d1+5e-11];
>> d2==d1
ans =
0 1
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Peter Perkins am 27 Jul. 2015
Or, in R2014b or later,
>> d = datetime({'12/25/1957' '12/25/1960'})
d =
25-Dec-1957 25-Dec-1960
>> day(d,'dayofyear')
ans =
359 360
>> cellstr(d,'yyyy-D')
ans =
'1957-359' '1960-360'
K E am 19 Mai 2016
Bearbeitet: K E am 19 Mai 2016
Wish I could vote on comments like StackOverflow - Peter's was useful for me.

Melden Sie sich an, um zu kommentieren.

Toni am 24 Feb. 2022
If you need convert a MATLAB datetime to a string with day-of-year,
The datestr function does not provide a format code for day-of-year. For example:
dt = datetime(2022,2,24)
datestr(dt,'yyyy-DDD')
results in: 2022-Thu
However, the string function can be used:
string(dt,'yyyy-DDD')
results in: 2022-055
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Peter Perkins am 2 Mär. 2022
Right, and in fact datestr on a datetime is just for backwards compatibility. Even when the input is a datetime, the format is interpreted as an "old-style" datestr format. Best to not use datestr any more!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by