Convert cell array datetime format to double array in HH:MM:SS format

4 views (last 30 days)
Hi,
I have a cell array for datetime format of 1441 x 1 as:
'2018-07-09 23:59:00.064834'
'2018-07-10 00:00:00.064833'
'2018-07-10 00:01:00.064829'
'2018-07-10 00:02:00.064832'
I want to express this array into 1441 x 1 double format but in the format of HH:MM:SS only
Can you help please?

Answers (1)

Steven Lord
Steven Lord on 23 Jul 2020
Do you want the numbers as a double array (and if so, what does that double array represent? Seconds since a specific time? Seconds after midnight on the day the date and time information represents?) or do you want the numbers as a duration array?
% Raw text data
X = {'2018-07-09 23:59:00.064834'
'2018-07-10 00:00:00.064833'
'2018-07-10 00:01:00.064829'
'2018-07-10 00:02:00.064832'};
% Create a datetime array from the text
% Make it display in the same format as the text data
dt = datetime(X, 'Format', 'dd-MMM-uuuu HH:mm:ss.SSSSSS')
% Subtract midnight from the datetime array to give a duration
previousMidnight = dateshift(dt, 'start', 'day');
du = dt - previousMidnight;
% Tweak the format so you can see we haven't lost the fractional seconds
du.Format = 'hh:mm:ss.SSSSSS'
% du is in the right format (with the fractional seconds added) but it is not a double array
% Convert the duration into a number of seconds past midnight
s = seconds(du)
% s is not in the format you asked for but it is a double array
  5 Comments
Steven Lord
Steven Lord on 23 Jul 2020
Can you give the name of the model (or post a link to the documentation page or example) that you're using as inspiration for what you're trying to do?
It sounds like you do want the number of seconds since an epoch. In this case I'll use the first (chronologically) date and time in X. [It happens for this data set that's also the first element in the array, but min will handle the case where the data set is out of order.]
% Raw text data
X = {'2018-07-09 23:59:00.064834'
'2018-07-10 00:00:00.064833'
'2018-07-10 00:01:00.064829'
'2018-07-10 00:02:00.064832'};
% Create a datetime array from the text
dt = datetime(X);
% Find the first/earliest date and time in the array
earliest = min(dt);
% Count the number of seconds since the earliest point in the array
s = seconds(dt-earliest)
Ahmed Darwish
Ahmed Darwish on 23 Jul 2020
the model name is power_V2G , just type that on matlab command and you will see the model, inside the irradirance block, you will find the lookup table and you will see how he already make the time as double array for 24 hour day with only decimal number

Sign in to comment.

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!