# Convert DoY and hour to datetime format

6 views (last 30 days)
Benju Baniya on 1 Jul 2022
Commented: Benju Baniya on 1 Jul 2022
I am very new to matlab and I am working with timeseries data. In the attached example, I want to add a new column as Datetime that displays datetime in the format year, month, day, hour, minute (YYYYMMDDHHMM). Your help will be much appreciated. Thanks.
Year DoY Hour Datetime
2013 1 0.5 201301010030

Abhishek Tiwari on 1 Jul 2022
Hi,
It can be done by using datetime() with modified display format as demonstrated
T.Datetime = datetime(T.Year, 1, T.DoY, floor(T.Hour), rem(T.Hour, 1).*60, 0, 'format', 'yyyyMMddHHmm')
T = 87648×4 table
Year DoY Hour Datetime ____ ___ ____ ____________ 2013 1 0.5 201301010030 2013 1 1 201301010100 2013 1 1.5 201301010130 2013 1 2 201301010200 2013 1 2.5 201301010230 2013 1 3 201301010300 2013 1 3.5 201301010330 2013 1 4 201301010400 2013 1 4.5 201301010430 2013 1 5 201301010500 2013 1 5.5 201301010530 2013 1 6 201301010600 2013 1 6.5 201301010630 2013 1 7 201301010700 2013 1 7.5 201301010730 2013 1 8 201301010800
These might be useful:
Benju Baniya on 1 Jul 2022
Thank you. It worked. This was the exact format I was looking for.

Eric Sofen on 1 Jul 2022
The datetime constructor is forgiving about day values that don't fit within a particular month and wrapping them appropriately:
t = 87648×3 table
Year DoY Hour ____ ___ ____ 2013 1 0.5 2013 1 1 2013 1 1.5 2013 1 2 2013 1 2.5 2013 1 3 2013 1 3.5 2013 1 4 2013 1 4.5 2013 1 5 2013 1 5.5 2013 1 6 2013 1 6.5 2013 1 7 2013 1 7.5 2013 1 8
t.DT = datetime(t.Year,1,t.DoY) + hours(t.Hour)
t = 87648×4 table
Year DoY Hour DT ____ ___ ____ ____________________ 2013 1 0.5 01-Jan-2013 00:30:00 2013 1 1 01-Jan-2013 01:00:00 2013 1 1.5 01-Jan-2013 01:30:00 2013 1 2 01-Jan-2013 02:00:00 2013 1 2.5 01-Jan-2013 02:30:00 2013 1 3 01-Jan-2013 03:00:00 2013 1 3.5 01-Jan-2013 03:30:00 2013 1 4 01-Jan-2013 04:00:00 2013 1 4.5 01-Jan-2013 04:30:00 2013 1 5 01-Jan-2013 05:00:00 2013 1 5.5 01-Jan-2013 05:30:00 2013 1 6 01-Jan-2013 06:00:00 2013 1 6.5 01-Jan-2013 06:30:00 2013 1 7 01-Jan-2013 07:00:00 2013 1 7.5 01-Jan-2013 07:30:00 2013 1 8 01-Jan-2013 08:00:00
% Check that it looks reasonable further along...
t(1000:100:end,:)
ans = 867×4 table
Year DoY Hour DT ____ ___ ____ ____________________ 2013 21 20 21-Jan-2013 20:00:00 2013 23 22 23-Jan-2013 22:00:00 2013 26 0 26-Jan-2013 00:00:00 2013 28 2 28-Jan-2013 02:00:00 2013 30 4 30-Jan-2013 04:00:00 2013 32 6 01-Feb-2013 06:00:00 2013 34 8 03-Feb-2013 08:00:00 2013 36 10 05-Feb-2013 10:00:00 2013 38 12 07-Feb-2013 12:00:00 2013 40 14 09-Feb-2013 14:00:00 2013 42 16 11-Feb-2013 16:00:00 2013 44 18 13-Feb-2013 18:00:00 2013 46 20 15-Feb-2013 20:00:00 2013 48 22 17-Feb-2013 22:00:00 2013 51 0 20-Feb-2013 00:00:00 2013 53 2 22-Feb-2013 02:00:00
Benju Baniya on 1 Jul 2022
Thank you. It worked.

### Categories

Find more on Calendar 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!

Translated by