Transformation from datetime to seconds
조회 수: 72 (최근 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
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!