Changing gregorian date number to calendar date and time

91 views (last 30 days)
I have an 18 years monthly data, whose 'time' variable is in gregorian calender. How do I change this to YYYY-MM format
More details about the variable:
units = hours since 1900-01-01 00:00:00.0
long_name = time
calendar = gregorian
For reference this is what time(1) looks like
>> time(1)
ans =
876582

Accepted Answer

Akira Agata
Akira Agata on 6 Dec 2019
If your time vector represents hours since 1900-01-01 00:00:00.0 , following code can convert it into yyyy-MM-dd.
T = datetime(1900,1,1) + hours(time);
T.Format = 'yyyy-MM-dd';
  3 Comments
Carrie Merritt
Carrie Merritt on 1 Dec 2020
what if you have, say, 3 hourly data instead of monthly?
time
Size: 13208x1
Dimensions: record
Datatype: double
Attributes:
standard_name = 'time'
long_name = 'Time'
units = 'hours since 1979-12-01 00'
time_calendar = 'gregorian'
start = '1979120100'
step = '3'
With having a time step of '3' I'm not sure how to apply the datetime function.
For reference:
>> time(1)
ans =
176064
Akira Agata
Akira Agata on 1 Dec 2020
The same process should work:
% Sample time vector
time = [176064; 176067; 176070];
% Convert to datetime vector
T = datetime(1900,1,1) + hours(time);
% Set the display format to show hours
T.Format = 'yyyy-MM-dd HH:mm:ss';
>> T
T =
1920-02-02 00:00:00
1920-02-02 03:00:00
1920-02-02 06:00:00

Sign in to comment.

More Answers (0)

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!