MATLAB Answers

How can I convert datetime vector to datenum (class-double)

35 ビュー (過去 30 日間)
Farshid Daryabor
Farshid Daryabor 2020 年 4 月 28 日
回答済み: Peter Perkins 2020 年 4 月 30 日
Please find attached file, time vector is original format in double I have, for some reason (preprocessing analysis) I have converted it to datetime format (Time) using below command. Again I want to convert Time to the original format (ie, class-double) according to the Pivot Year (1950-01-01). I really appreciate if someone can help me.
Time = datetime (time * 60 * 60 * 24, 'ConvertFrom', 'epochtime', 'Epoch', '1950-01-01');

  3 件のコメント

KSSV
KSSV 2020 年 4 月 28 日
iwant = datenum(Time) ;
Farshid Daryabor
Farshid Daryabor 2020 年 4 月 28 日
I tried this one, but cannot be exactly same original one. You can test it by converting "iwant" using below commend to datetime is not what I expect.
Time = datetime(iwant*60*60*24,'ConvertFrom','epochtime','Epoch','1950-01-01');
see form and year
Farshid Daryabor
Farshid Daryabor 2020 年 4 月 28 日
Dear Ameer,
Thanks very good

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 28 日
try this
pvtYear = datetime(1950, 01, 01);
time_ = datenum(Time-pvtYear); % time_ is same as time, minor differences probably due to floating point errors
Result:
>> all(ismembertol(time_,time))
ans =
logical
1

  2 件のコメント

Farshid Daryabor
Farshid Daryabor 2020 年 4 月 28 日
Thanks Ameer, very good
Ameer Hamza
Ameer Hamza 2020 年 4 月 28 日
Glad to be of help.

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

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2020 年 4 月 30 日
You almost certainly DO NOT want to convert datetimes to datenums. In recent versions of MATLAB, there are not many good reasons to want to use datenum at all.
If you mean, "I need to convert back to elapsed time since 1950", that's maybe a different story, but those are not "datenums". Here's a simple way to do it:
dt = days(myDatetimes - datetime(1950,1,1))
However, if your values are counting days since 1950, you are shooting yourself in the foot -- you will never have an exact representation of time within day because 1/24, 1/1440. 1/86400, none of them are representable exactly in double precision.
Unless your timestamps are always at 00:00:00, 03:00:00, etc., you would be much better off using seconds.

  0 件のコメント

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

Community Treasure Hunt

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

Start Hunting!

Translated by