How to find the day number of any year from a date?
조회 수: 163 (최근 30일)
이전 댓글 표시
I am very new in programming and therefore it is probably a novice's question. I have to write a script that takes a number of day(dd), month(mm) and year(yyyy)values arranged in three separate columns of a MATLAB variable and gives the day number of the year(e.g. for 03 March 2012, 31+29+3=63) as output.
댓글 수: 2
채택된 답변
Peter Perkins
2015년 7월 23일
If you are using R2014b or later, the datetime data type allows you to compute the day of year using the day function:
>> x = [2012 1 2; 2012 3 3; 2013 3 3]
x =
2012 1 2
2012 3 3
2013 3 3
>> d = datetime(x)
d =
02-Jan-2012
03-Mar-2012
03-Mar-2013
>> day(d,'dayofyear')
ans =
2
63
62
댓글 수: 2
Peter Perkins
2015년 7월 24일
Presumably, you are not "using R2014b or later". datetime did not exist prior to that.
추가 답변 (3개)
Andrei Bobrov
2015년 7월 24일
x = [2012 1 2; 2012 3 3; 2013 3 3];
d = datenum(x);
out = d - datenum(year(d),1,1) + 1;
댓글 수: 0
Sergey Kostrukov
2022년 2월 12일
편집: Sergey Kostrukov
2022년 2월 12일
Fractional value of days since biginning of the year (0-364.9999):
daysFrac = days(currentTime - datetime(year(currentTime), 1, 1, TimeZone=currentTime.TimeZone))
To get the whole number starting from 1 (i.e. day index):
floor(daysFrac + 1)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!