Converting cell array to matlab datetime format

조회 수: 112 (최근 30일)
AA
AA 2020년 4월 9일
답변: Mohammad Sami 2020년 4월 9일
Hi,
I have a cell array with dates like this '03.08.2003 23:00:00.000 GMT+0200'
I want the cell array to be converted to a datetime array. Has anyone got a suggestion for that?
  댓글 수: 3
AA
AA 2020년 4월 9일
>> dt = datetime('03.08.2003 23:00:00.000 GMT+0200', 'InputFormat', 'dd.MM.yyyy HH:mm')
Error using datetime (line 616)
Unable to parse '03.08.2003 23:00:00.000 GMT+0200'
as a date/time using the format 'dd.MM.yyyy HH:mm'.
AA
AA 2020년 4월 9일
I get an error as above. I need for datetime to ignore .000 GMT+0200

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

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 4월 9일
we can replace the gmt with utc
t = {'03.08.2003 23:00:00.000 GMT+0200'};
t = strrep(t,'GMT','UTC');
t = datetime(t,'InputFormat','dd.MM.uuuu HH:mm:ss.SSS ZZZZZ','TimeZone','UTC');
t.TimeZone = '+02:00';

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 4월 9일
편집: Ameer Hamza 2020년 4월 9일
It depends on how you are trying to save the datetime values. For example, this convert save all values in UTC
A = {'03.08.2003 23:00:00.000 UTC+0200', ...
'07.08.2003 10:00:00.000 UTC+0300', ...
'09.08.2003 15:00:00.000 UTC+0500'};
dt = datetime(A, 'InputFormat', 'dd.MM.yyyy HH:mm:ss.SSS ZZZZ', 'TimeZone', 'UTC')
dt =
1×3 datetime array
03-Aug-2003 21:00:00 07-Aug-2003 07:00:00 09-Aug-2003 10:00:00
Following will convert to cell array of datetime values with local time zones
A = {'03.08.2003 23:00:00.000 UTC+0200', ...
'07.08.2003 10:00:00.000 UTC+0300', ...
'09.08.2003 15:00:00.000 UTC+0500'};
offset = cellfun(@(x) {x(end-4:end)}, A);
dt = cellfun(@(D,Z) {datetime(D, 'InputFormat', 'dd.MM.yyyy HH:mm:ss.SSS ZZZZ', 'TimeZone', Z)}, A, offset)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by