# Convert netcdf time ('minutes since 2013-2-1 00:00:00') to actual date

4 views (last 30 days)
Marzuki Marzuki on 2 Nov 2021
Commented: Marzuki Marzuki on 2 Nov 2021
Hi all;
I have netcdf file. When I use ncdisp on the netcdf file, time is described as the following:
XTIME
Size: 22177x1
Dimensions: XTIME
Datatype: double
Attributes:
standard_name = 'time'
units = 'minutes since 2013-2-1 00:00:00'
calendar = 'standard'
axis = 'T'
60
120
180
240
300
360
420
...
I used the following code to convert XTIME to actual date,
t1 = double(netcdf_time) + datenum('2013-2-1 00:00:00');
t2 = datevec(t1);
However, the result is not correct, as below
2013 4 2 0 0 0
2013 6 1 0 0 0
2013 7 31 0 0 0
...
Actual date should be hourly time fomat.
If any of friends here have any experience with this problem, please share it. I really thank you for that.

Steven Lord on 2 Nov 2021
I recommend using datetime rather than datenum.
netcdf_time = 60:60:300 % Sample data
netcdf_time = 1×5
60 120 180 240 300
epoch = datetime(2013, 1, 2) % Assuming January 2nd, swap the 1 and 2 for February 1st
epoch = datetime
02-Jan-2013
T = epoch + minutes(netcdf_time)
T = 1×5 datetime array
02-Jan-2013 01:00:00 02-Jan-2013 02:00:00 02-Jan-2013 03:00:00 02-Jan-2013 04:00:00 02-Jan-2013 05:00:00
Marzuki Marzuki on 2 Nov 2021
Thank you @Steven Lord It works

Bjorn Gustavsson on 2 Nov 2021
Remember that datenum is in (fractional) days, while your netcdf-time is in minutes. It seems likely that you want to do:
t1 = double(netcdf_time)/60/24 + datenum('2013-2-1 00:00:00');
HTH
Marzuki Marzuki on 2 Nov 2021
Thank you @Bjorn Gustavsson it works too.

### Community Treasure Hunt

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

Start Hunting!