从Datetime转换为Unix时间 [英] Convert From Datetime To Unix Time
本文介绍了从Datetime转换为Unix时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了将UNIX时间转换为DaTime的功能,无论如何我现在需要将函数从DateTime转换为UNIX时间戳,但这有点错误因为当我以某种方式传递DateTime时,不显示最后3位数字例如我有这个时间戳:
I got function which is converting UNIX time to DaTime which is working great anyhow now i need function to convert from DateTime to UNIX timestamp but something is wrong with this because when i am passing DateTime somehow the last 3 digits are not shown for instance i have this timestamp:
1300124700345
等于:
which is equal to:
14.03.2011 5:45 PM
我将其传递给以下功能我正在检索:
When i pass that to below function i am retreiving:
1300124700
3个最后的数字总是以某种方式消耗掉。你能在这帮吗?
3 last digits dissaperd somehow always. Could you help here?
static readonly DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public static long ToUnixTime(DateTime date)
{
return Convert.ToInt64((date - UnixEpoch).TotalSeconds);
}
推荐答案
http://en.code-bude.net/2013/09/22/how-to-convert-csharp-datetime- ticks-into-unix-timestamp / [ ^ ]
您只使用秒。尝试将其添加为像这样的毫米级:
You are only using seconds. Try to add it as millisecornds like this:
public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(unixTimeStamp));
}
祝你好运!
Good luck!
这篇关于从Datetime转换为Unix时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文