How to transform a cell array, which contains dates but not in the datetime format, into datetime array

3 views (last 30 days)
Hello to everybody.
The cell array contains values like this one :
x={'1991-01-09T02:00:00+00:00' ; '1991-01-09T03:00:00+00:00' ; '1991-01-09T04:00:00+00:00 .......}
and they are always in this format.
My goal is to obtain a hourly datetime array from it in the format 'yyyy-MM-dd''T''HH' or something similar, what's important is that it has also hours, i don't absolutely need minutes and seconds, just hours.
I need this solution because the cell array comes from an external textfile (it is a simulation output), and the dates in it are not everywhere perfectly regular hour by hour, so i don't need to just create a hourly datetime array myself, but i really would like to scan that cell array in some way and create a datetime array with the ones present in the original with years-months-days and hours.
Hope that my question is clear! Thank you!

Accepted Answer

dpb
dpb on 29 Jan 2021
Edited: dpb on 29 Jan 2021
>> dt=datetime(tstr,'InputFormat','yyyy-MM-dd''T''hh:mm:ssXXX','TimeZone','America/Chicago')
dt =
3×1 datetime array
08-Jan-1991 20:00:00
08-Jan-1991 21:00:00
08-Jan-1991 22:00:00
>>
Fixup the time zone to match or convert to be unzoned by setting its 'TimeZone' property to '' (empty string).
>> dt.TimeZone=''
dt =
3×1 datetime array
08-Jan-1991 20:00:00
08-Jan-1991 21:00:00
08-Jan-1991 22:00:00
>>
Set format as want via:
>> dt.Format='dd-MMM-uuuu HH'
dt =
3×1 datetime array
08-Jan-1991 20
08-Jan-1991 21
08-Jan-1991 22
>>
  2 Comments
Enrico Gambini
Enrico Gambini on 29 Jan 2021
It works! One only comment, the 'InputFormat should be:
'yyyy-MM-dd''T''HH:mm:ssXXX' %%capital h instead
Otherwise dates going from 12 am to 12 pm will be saved as NaT.
Thank you a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!