将 DateTime 转换为 EPOCH 时间返回一个新的日期
Converting DateTime to EPOCH time is returning a new Date
要将日期时间转换为 EPOCH 时间,我正在使用以下函数:
public static long EpochTime(DateTime dt)
{
//long form code to be clear
TimeSpan t = dt.ToLocalTime() - new DateTime(1970, 1, 1);
long millisecondsSinceEpoch = (long)t.TotalSeconds * 1000;
return millisecondsSinceEpoch;
}
现在如果我测试输入日期为 15/07/2018 1:09:42 PM
的函数,输出日期变为
GMT: Sunday, July 15, 2018 11:09:42 PM
Your time zone: Monday, July 16, 2018 9:09:42 AM GMT+10:00
我需要纪元时间,因为 Highchart 需要 x 轴上的纪元时间。
您需要确保比较的是正确时区的时间。我建议将两者都转换为 UTC:
TimeSpan t = dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
要将日期时间转换为 EPOCH 时间,我正在使用以下函数:
public static long EpochTime(DateTime dt)
{
//long form code to be clear
TimeSpan t = dt.ToLocalTime() - new DateTime(1970, 1, 1);
long millisecondsSinceEpoch = (long)t.TotalSeconds * 1000;
return millisecondsSinceEpoch;
}
现在如果我测试输入日期为 15/07/2018 1:09:42 PM
的函数,输出日期变为
GMT: Sunday, July 15, 2018 11:09:42 PM
Your time zone: Monday, July 16, 2018 9:09:42 AM GMT+10:00
我需要纪元时间,因为 Highchart 需要 x 轴上的纪元时间。
您需要确保比较的是正确时区的时间。我建议将两者都转换为 UTC:
TimeSpan t = dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);