Changing gregorian date number to calendar date and time

88 vues (au cours des 30 derniers jours)
Arun Nair
Arun Nair le 6 Déc 2019
Commenté : Akira Agata le 1 Déc 2020
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

Réponse acceptée

Akira Agata
Akira Agata le 6 Déc 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 commentaires
Carrie Merritt
Carrie Merritt le 1 Déc 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 le 1 Déc 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by