Transformation from datetime to seconds
229 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Adam Danz
2023-3-31
> 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)'
TimeAbsSeconds = datetime(TimeAbs,'Format','ss.S')
TimeAbsDiff = diff(TimeAbs)
seconds(TimeAbsDiff)
1 个评论
Peter Perkins
2023-4-5
Or even TimeAbs.Second to get the second component of the timestamps.
Cognator, to "transform to seconds", you have to say, "seconds from where?"
Steven Lord
2023-3-31
How do you want to perform this transformation? What does right now translate to in seconds?
N = datetime('now')
Do you want to calculate the number of seconds since some fixed time that the datetime occurred?
midnight = datetime(2023, 3, 31) % or
midnight = dateshift(N, 'start', 'day')
We can create a duration that represents how long elapsed between midnight and now.
elapsed = N - midnight
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
elapsed.Format = 's' % Just change the format
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
That looks like reasonably close to the value I received in elapsed and elapsedSeconds.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!