char time: wrong label on years

8 views (last 30 days)
Sofia Stella
Sofia Stella on 10 Dec 2019
Answered: Sofia Stella on 11 Dec 2019
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

Answers (2)

dpb
dpb on 10 Dec 2019
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 Comments
Steven Lord
Steven Lord on 10 Dec 2019
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 on 10 Dec 2019
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...

Sign in to comment.


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

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!