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

조회 수: 3 (최근 30일)
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!

채택된 답변

dpb
dpb 2021년 1월 29일
편집: dpb 2021년 1월 29일
>> 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
Enrico Gambini
Enrico Gambini 2021년 1월 29일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by