3

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.

Orace
  • 7,822
  • 30
  • 45
Seba Ja
  • 41
  • 1
  • 5

1 Answers1

5

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);
}
Orace
  • 7,822
  • 30
  • 45
  • 1
    Don't forget `DateTimeOffset.FromUnixTimeMilliseconds` https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimemilliseconds?view=net-5.0#System_DateTimeOffset_FromUnixTimeMilliseconds_System_Int64_ – pinkfloydx33 Mar 14 '21 at 15:35
  • 2
    As note by pinkfloydx33, you can use `DateTimeOffset.FromUnixTimeMilliseconds`. [Here](https://stackoverflow.com/a/66609223/7444103), for example, in a custom `UnixDateTimeConverter`, deserializing the JSON APIs' results – Jimi Mar 14 '21 at 15:42
  • Thank You very much for Your answers. Example works, didnt know that this is the format I needed to look for. – Seba Ja Mar 14 '21 at 17:15