Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active February 27, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/44f687da55126eb344316754e2b2e9fd to your computer and use it in GitHub Desktop.
Save guitarrapc/44f687da55126eb344316754e2b2e9fd to your computer and use it in GitHub Desktop.
UnixtimeStamp and .NET DateTime converter.
// if you are using .NET 4.6 and higher, use `DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()`
private double GetUnixTimeStamp(DateTime date)
{
return date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
// if you are using .NET 4.6 and higher, use `DateTimeOffset.FromUnixTimeMilliseconds(unixtimestamp)`
private DateTime GetDateTime(double unixtimestamp)
{
return new DateTime(1970, 1, 1).AddMilliseconds(unixtimestamp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment