C# version of Javascript Date.getTime()

前端 未结 8 1299
予麋鹿
予麋鹿 2021-02-01 21:11

What is the best way in c# to get the same result of javascript date.gettime() call?

The getTime() method returns the number of milliseconds since midnigh

8条回答
  •  一整个雨季
    2021-02-01 21:41

    The correct implementation (assuming the current time) is as follows:

    DateTime utcNow = DateTime.UtcNow;
    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    long ts = (long)((utcNow - epoch).TotalMilliseconds);
    

提交回复
热议问题