C# version of Javascript Date.getTime()

前端 未结 8 1298
予麋鹿
予麋鹿 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:23

    I guess this will do the trick :)

    public double MilliTimeStamp(DateTime TheDate)
            {
                DateTime d1 = new DateTime(1970, 1, 1);
                DateTime d2 = TheDate.ToUniversalTime();
                TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    
                return ts.TotalMilliseconds;
            }
    

提交回复
热议问题