How to go from datetime (yyyy:mm: dd hh:mm:ss) to seconds?

7 ビュー (過去 30 日間)
Ilse Frans
Ilse Frans 2023 年 7 月 4 日
コメント済み: Peter Perkins 2023 年 7 月 17 日
I have a timestamp variable with the following output (size: 1207x1):
'2023-06-27 08:31:05'
'2023-06-27 08:31:06'
'2023-06-27 08:31:07'
'2023-06-27 08:31:08'
etc.
How do I turn this into a variable where it is only seconds?
I tried the function datevec, but after every minute the seconds get to zero again, but I want the amount of seconds to continue after a minute.
Thanks in advance!
  4 件のコメント
Stephen23
Stephen23 2023 年 7 月 4 日
編集済み: Stephen23 2023 年 7 月 5 日
MATLAB does not have a TIME2NUM function. The Predictive Maintenance toolbox has such a function.
The MATLAB approach is to use a DATETIME variable and then call TIMEOFDAY and SECONDS (all of which are actual MATLAB functions):
C = {'2023-06-27 08:31:05';'2023-06-27 08:31:06';'2023-06-27 08:31:07';'2023-06-27 08:31:08'};
T = datetime(C)
T = 4×1 datetime array
27-Jun-2023 08:31:05 27-Jun-2023 08:31:06 27-Jun-2023 08:31:07 27-Jun-2023 08:31:08
V = seconds(timeofday(T))
V = 4×1
30665 30666 30667 30668
Peter Perkins
Peter Perkins 2023 年 7 月 17 日
Or maybe "seconds since some origin"
C = ["2023-06-27 08:31:05";"2023-06-27 08:31:06";"2023-06-27 08:31:07";"2023-06-27 08:31:08"];
T = datetime(C)
T = 4×1 datetime array
27-Jun-2023 08:31:05 27-Jun-2023 08:31:06 27-Jun-2023 08:31:07 27-Jun-2023 08:31:08
s = T - "1-Jan-2023"; s.Format = 's'
s = 4×1 duration array
1.5323e+07 sec 1.5323e+07 sec 1.5323e+07 sec 1.5323e+07 sec
seconds(s) % as numeric
ans = 4×1
15323465 15323466 15323467 15323468

サインインしてコメントする。

採用された回答

Ilse Frans
Ilse Frans 2023 年 7 月 4 日
Nevermind, tried the function time2num and worked :)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by