How to convert datetime to day of the year when there are more than one year?

조회 수: 24 (최근 30일)
Hi!
If I have an array of datetime like this -
'2003-10-14'
'2003-11-07'
'2003-11-15'
'2003-11-23'
'2004-01-10'
'2004-04-07'
'2004-10-08'
'2005-01-04'
'2005-01-20'
'2005-05-28'
'2005-06-05'
'2005-09-17'
'2005-11-20'
'2006-02-08'
'2006-02-16'
'2006-02-24'
'2006-03-04'
'2006-03-12'
How can I convert this datetime array as the day of the year? My day of the year should start from 2003-01-01 (= day 01). Once I reach 2004-01-01, it should be day 366. For the 2005-01-01, it should be day 731 and so on. Can any one kindly tell me how can I convert the whole array into a day of the year array?
Any feedback from you will be highly appreciated!!

채택된 답변

KSSV
KSSV 2022년 12월 20일
  댓글 수: 2
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 20일
Hi! Thank you for the link. But the problem is, when my date is 2003-01-10 and 2004-01-10 the conversion shows it's the 10th day of the year (which is correct for individual year). But for my calculation, I need it to be 10 and 375.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 12월 20일
Let's look at your sample data.
data = {'2003-10-14'
'2003-11-07'
'2003-11-15'
'2003-11-23'
'2004-01-10'
'2004-04-07'
'2004-10-08'
'2005-01-04'
'2005-01-20'
'2005-05-28'
'2005-06-05'
'2005-09-17'
'2005-11-20'
'2006-02-08'
'2006-02-16'
'2006-02-24'
'2006-03-04'
'2006-03-12'};
dt = datetime(data);
What's the first date in your data array? I'm not going to assume it's the first element of data.
firstdate = min(dt)
firstdate = datetime
14-Oct-2003
What's the start of that first date's year?
firstOfYear = dateshift(firstdate, 'start', 'year')
firstOfYear = datetime
01-Jan-2003
How many calendar days have elapsed between the 1st of January of that earliest year and each element of your data?
elapsedTime = between(firstOfYear, dt, 'Days')
elapsedTime = 18×1 calendarDuration array
286d 310d 318d 326d 374d 462d 646d 734d 750d 878d 886d 990d 1054d 1134d 1142d 1150d 1158d 1166d
Let's convert that into double. Note I need to add 1 because elapsedTime is the time between January 1st and your data but you want the number of those dates. There's 0 days between January 1st and January 1st but January 1st is day 1 of the year.
d = caldays(elapsedTime)+1
d = 18×1
287 311 319 327 375 463 647 735 751 879
  댓글 수: 1
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 20일
편집: Ashfaq Ahmed 2022년 12월 20일
Excellent!! I was manually adding 365 days once I have a new year and my code looked that big -
DOY2 = day(dt,'dayofyear');
DOY2(19:48) = 365*1+DOY2(19:48);
DOY2(49:85) = 365*2+DOY2(49:85);
DOY2(86:117) = 365*3+DOY2(86:117);
DOY2(118:150) = 365*4+DOY2(118:150);
DOY2(151:183) = 365*5+DOY2(151:183);
DOY2(184:217) = 365*6+DOY2(184:217);
DOY2(218:249) = 365*7+DOY2(218:249);
DOY2(250:279) = 365*8+DOY2(250:279);
DOY2(280:293) = 365*9+DOY2(280:293);
DOY2(294:328) = 365*10+DOY2(294:328);
DOY2(329:366) = 365*11+DOY2(329:366);
DOY2(367:401) = 365*12+DOY2(367:401);
DOY2(402:436) = 365*13+DOY2(402:436);
DOY2(437:470) = 365*14+DOY2(437:470);
DOY2(471:501) = 365*15+DOY2(471:501);
DOY2(502:540) = 365*16+DOY2(502:540);
DOY2(541:574) = 365*17+DOY2(541:574);
DOY2(575:610) = 365*18+DOY2(575:610);
You just made the whole calculation so easy and smart. Great thinking process!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by