C# version of Javascript Date.getTime()
You can use this solution:
private int GetTime()
{
var time = (DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1));
return (int)(time.TotalMilliseconds + 0.5);
}
Since JavaScript time is with respect to UTC, I think you will need something like this:
var st = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var t = (DateTime.Now.ToUniversalTime() - st);
// t.TotalMilliseconds
Now you can use the TotalMilliseconds
property of the Timespan
.
The Java and JavaScript Date.getTime() methods return the number of milliseconds since 1 Jan 1970 00:00:00 GMT.
Since .NET represents dates in Ticks (1 Tick = 0.1 nanoseconds or 0.0001 milliseconds) since 1 Jan 0001 00:00:00 GMT, we must use a conversion formula where 621355968000000000 is the offset between the base dates in Ticks and 10000 the number of Ticks per Millisecond.
Ticks = (MilliSeconds * 10000) + 621355968000000000
MilliSeconds = (Ticks - 621355968000000000) / 10000