问题
public static long GetUTC8TimeStamp(DateTime dateTime)
{
return new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();
}
使用DateTimeOffset(DateTime).ToUnixTimeMilliseconds() 获取时间戳时出现ArgumentOutOfRangeException: The UTC time represented when the offset is applied must be between year 0 and 10,000. 的问题。
解决
dateTime 使用了默认值导致的问题,加入判断即可解决。
if (dateTime == DateTime.MinValue)
dateTime = DateTime.Now;
拓展
/// <summary>
/// 获取13位时间戳
/// </summary>
/// <returns></returns>
public static long GetUTC8TimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
return Convert.ToInt64(ts.TotalMilliseconds);
}