Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Binance timestamp into valid datetime

Tags:

c#

binance

I dont know how to covert timestamp given by Binance server into valid DateTime.

Binance.API.Csharp.Client.Models.General.ServerInfo returns 1615724572987 which after conversion to DateTime gives 1/2/0001 9:52:52 PM which obviously is not correct.

I tried to find description about ServerInfo type but there is only GetHtml Function.

like image 992
Seba Ja Avatar asked Mar 25 '26 15:03

Seba Ja


1 Answers

From this question you will learn that

"[In binance API] All time and timestamp related fields are in milliseconds." (unix style)

And from this question you will learn to convert unix timestamp to DateTime.

Then combine this knowledge to create this method:

public static DateTime BinanceTimeStampToUtcDateTime(double binanceTimeStamp)
{
    // Binance timestamp is milliseconds past epoch
    var epoch = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
    return epoch.AddMilliseconds(binanceTimeStamp);
}
like image 151
Orace Avatar answered Mar 27 '26 05:03

Orace