char time: wrong label on years
Show older comments
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:
Answers (2)
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
on 10 Dec 2019
There's no need to convert back from a datetime to a datevec and back to a datetime just to add 2000 calendar years.
dt = datetime('24-May-0014')
dt2 = dt + calyears(2000)
dpb
on 10 Dec 2019
I wondered about that...apparently I did something wrong or just never did get around to try the addition of a duration, Steven.
Thanks for pointing it out...
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
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...
Sofia Stella
on 11 Dec 2019
0 votes
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!