why cannot generate the sequence of Years

>> s=datetime('2014','Format','y');
ss=datetime('2019','Format','y');
t1 =s:years(1):ss;
t2= s+ years(1:5);
%
disp(t1);
disp(t2);
2014 2015 2016 2016 2017
2015 2016 2016 2017 2019

댓글 수: 1

Dai Chao
Dai Chao 2023년 5월 4일
why t1 is not 2014 2015 2016 2017 2018 and 2019, and why t2 is not 2015 2016 2017 2018 and 2019?

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

 채택된 답변

Steven Lord
Steven Lord 2023년 5월 4일
Let's look at your vectors in a different format, with day and month information not just year information.
s=datetime('2014','Format','y');
ss=datetime('2019','Format','y');
t1 =s:years(1):ss; t1.Format = 'dd-MMM-yyyy'
t1 = 1×5 datetime array
01-Jan-2014 01-Jan-2015 01-Jan-2016 31-Dec-2016 31-Dec-2017
t2= s+ years(1:5); t2.Format = 'dd-MMM-yyyy'
t2 = 1×5 datetime array
01-Jan-2015 01-Jan-2016 31-Dec-2016 31-Dec-2017 01-Jan-2019
You want to use calendar years (which account for leap years) rather than years (which don't) as stated in the Description section of the years function.
t3 = s:calyears(1):ss; t3.Format = 'dd-MMM-yyyy'
t3 = 1×6 datetime array
01-Jan-2014 01-Jan-2015 01-Jan-2016 01-Jan-2017 01-Jan-2018 01-Jan-2019
t4 = s + calyears(1:5); t4.Format = 'dd-MMM-yyyy'
t4 = 1×5 datetime array
01-Jan-2015 01-Jan-2016 01-Jan-2017 01-Jan-2018 01-Jan-2019

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2023년 5월 4일

댓글:

2023년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by