ronsun
5/7/2018 - 7:43 AM

windows 18-digit ticks to Unix 13-digit ticks

windows 18-digit ticks to Unix 13-digit ticks

  • After .NET 4.6
long unixTimeSecond = DateTimeOffset.Now.ToUnixTimeSeconds();
long unixTimeMilliSecond = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  • Other way
long epochTicks = new DateTime(1970, 1, 1).Ticks;
long unixTimeSecond = ((DateTime.Now.Ticks - epochTicks) / TimeSpan.TicksPerSecond);
long unixTimeMilliSecond = ((DateTime.Now.Ticks - epochTicks) / TimeSpan.TicksPerMillisecond);