Convert Binance timestamp into valid datetime

Viewed 3189

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.

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);
}
Related