필터 지우기
필터 지우기

how to convert date to milliseconds (Binance format)

조회 수: 11 (최근 30일)
endystrike
endystrike 2021년 11월 30일
답변: Steven Lord 2021년 12월 1일
I got this function some time ago to convert datetime from Binance format into Matlab format, but now I need to do viceversa too, so to convert a certain datetime into Binance milliseconds format.
function [out_date] = date_from_binance_to_dt(raw_date)
out_date = string(datestr(datevec(raw_date/60/60/24/1000) + [1970 0 0 0 0 0]));
end
Can someone please help me to create the inverse function of mine one?
Thank you!
  댓글 수: 3
endystrike
endystrike 2021년 11월 30일
The only definition reported on Binance API documentation is the following: "All time and timestamp related fields are in milliseconds.".
In few words, I need to convert a MatLab datetime expression into milliseconds format, that is the one required by Binance API.
Thank you!
endystrike
endystrike 2021년 11월 30일
I've found a solution:
function [binance_dateFMT] = date_from_dt_to_BinanceFMT(date_DT) %input: Matlab datetime format
binance_dateFMT = floor((datenum(date_DT)-datenum('1970', 'yyyy'))*60*60*24*1000);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2021년 12월 1일
So your input is a number of milliseconds since January 1st, 1970?
msSince1970 = 1234567;
start = datetime(1970, 01, 01);
T1 = datetime(msSince1970,'ConvertFrom', ...
'epochtime', 'Epoch', start, 'TicksPerSecond', 1000)
T1 = datetime
01-Jan-1970 00:20:34
% or
T2 = start + seconds(msSince1970/1000)
T2 = datetime
01-Jan-1970 00:20:34
check = T1==T2
check = logical
1
To go backwards, creating msSince1970 from T1:
ms = 1000*seconds(T1-start)
ms = 1234567

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by