Convert.ToInt64(DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);
使用此代码获取到的时间戳通常会比实际时间早8小时,我们猜测原因可能是new DateTime(1970,1,1)返回的其实得到的是北京时间,即UTC+8的值。
而标准的时间戳是北京时间-8小时,综上,时间戳正确代码如下
Convert.ToInt64(DateTime.UTCNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);
得到正确时间