Transformation from datetime to seconds

228 views (last 30 days)
Coginator
Coginator on 31 Mar 2023
Commented: Peter Perkins on 5 Apr 2023
Hi,
I wanna do a transformation from datetime dd:MM:yyyy hh:mm:ss to seconds form a table.
this is my code (not the whole)
% Specify variable properties
opts = setvaropts(opts, "TimeAbs", "InputFormat", "dd.MM.yyyy HH:mm:ss");
% Import the data
tbl = readtable("Data.xlsx", opts);
%% Prepare data for calculations
TimeAbs = tbl.TimeAbs;
t = tbl.TimeRel; % [s]
I wanna make a vector TimeRel from the TimeAbs table
Thank you for your help

Answers (2)

Adam Danz
Adam Danz on 31 Mar 2023
> I wanna do a transformation from datetime dd:MM:yyyy hh:mm:ss to seconds from a table
See a table of format identifies in the documentation
It's not clear whether you want to merely change the format to seconds or compute the difference and show that in seconds. I've shown both below.
TimeAbs = datetime(22,1,1,2,3,20,'Format','dd.MM.yyyy HH:mm:ss')+seconds(1:10)'
TimeAbs = 10×1 datetime array
01.01.0022 02:03:21 01.01.0022 02:03:22 01.01.0022 02:03:23 01.01.0022 02:03:24 01.01.0022 02:03:25 01.01.0022 02:03:26 01.01.0022 02:03:27 01.01.0022 02:03:28 01.01.0022 02:03:29 01.01.0022 02:03:30
TimeAbsSeconds = datetime(TimeAbs,'Format','ss.S')
TimeAbsSeconds = 10×1 datetime array
21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 29.0 30.0
TimeAbsDiff = diff(TimeAbs)
TimeAbsDiff = 9×1 duration array
00:00:01 00:00:01 00:00:01 00:00:01 00:00:01 00:00:01 00:00:01 00:00:01 00:00:01
seconds(TimeAbsDiff)
ans = 9×1
1 1 1 1 1 1 1 1 1
  1 Comment
Peter Perkins
Peter Perkins on 5 Apr 2023
Or even TimeAbs.Second to get the second component of the timestamps.
Cognator, to "transform to seconds", you have to say, "seconds from where?"

Sign in to comment.


Steven Lord
Steven Lord on 31 Mar 2023
How do you want to perform this transformation? What does right now translate to in seconds?
N = datetime('now')
N = datetime
31-Mar-2023 13:46:55
Do you want to calculate the number of seconds since some fixed time that the datetime occurred?
midnight = datetime(2023, 3, 31) % or
midnight = datetime
31-Mar-2023
midnight = dateshift(N, 'start', 'day')
midnight = datetime
31-Mar-2023
We can create a duration that represents how long elapsed between midnight and now.
elapsed = N - midnight
elapsed = duration
13:46:55
If you need the number of seconds you can call seconds or (if you want to keep it a duration but change how it's presented) change the duration's Format property.
elapsedSeconds = seconds(elapsed) % or
elapsedSeconds = 4.9616e+04
elapsed.Format = 's' % Just change the format
elapsed = duration
49616 sec
As I type this elapsed is about 13 and 3/4 hours and elapsedSeconds is close to 50,000. Does that make sense?
estimate = 13.75*60*60 % 13.75 hours * 60 minutes/hour * 60 seconds/minute = units of seconds
estimate = 49500
That looks like reasonably close to the value I received in elapsed and elapsedSeconds.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!