Converting cell array to matlab datetime format
Show older comments
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 Comments
Mohammad Sami
on 9 Apr 2020
datetime support cellstring as input.
DateStrings — Text representing dates and times
character array | cell array of character vectors | string array
Use with the InputFormat flag. see documents for the formatting.
AA
on 9 Apr 2020
AA
on 9 Apr 2020
Accepted Answer
More Answers (1)
Ameer Hamza
on 9 Apr 2020
Edited: Ameer Hamza
on 9 Apr 2020
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)
Categories
Find more on Dates and Time 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!