char time: wrong label on years

조회 수: 3 (최근 30일)
Sofia Stella
Sofia Stella 2019년 12월 10일
답변: Sofia Stella 2019년 12월 11일
Hello,
I converted time from Gregorian days to classical label in dd/mm/yy in this way:
time_1= ncread ('/path/.nc', 'date_time');
time1=datestr(time_1,0);
but the label on YEARS is wrong, as you can see in the image. I have 0014, don't 2014!
Could you help me to have the right year?
thanks
example:
Screen Shot 2019-12-10 at 13.03.02.png

답변 (2개)

dpb
dpb 2019년 12월 10일
The problem is the explicit four-digit string for the year -- that overrides any pivot year input for either datenum/datestr as well as the recommended datetime.
You'll have to either fix up the input string or I'd recommend something like--
>> dtm=datetime('24-May-0014')
dtm =
datetime
24-May-0014
>> datetime(datevec(dtm)+[2000 0 0 0 0 0])
ans =
datetime
24-May-2014 00:00:00
>>
to fix up the year.
  댓글 수: 4
Steven Lord
Steven Lord 2019년 12월 10일
There are a few situations where a duration and a calendarDuration are slightly different.
dt = datetime('3-Mar-1999')
dt1 = dt + years(1) % March 2nd 2000 5:49:12
dt2 = dt + calyears(1) % March 3rd 2000
years(1) is 365.2425 days. calyears(1) is exactly one calendar year, taking into account leap days.
dpb
dpb 2019년 12월 10일
Ah, yes. I knew something made me react to use the integer via datevec even though I didn't have a conscious connection to what, precisely. calyears() never came to mind; not yet totally assimilated the new(ish) datetime class and friends...

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


Sofia Stella
Sofia Stella 2019년 12월 11일
At least I've solved in this way:
time0 = datenum(1970,1,1,0,0,0);
timeTOT = time+time0;
thank you all!

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by