这是我在 SQL Server 中做的示例
SELECT CAST(CAST('2012-01-25 10:00:00.000' AS DATETIME) AS INT) + 2
结果是 40933
如何使用 c# 实现这一点?这个整数是从哪里来的,又是怎么来的?
参考:从 DateTime 转换为 INT但这是我在 c# 中需要的
这是我在 SQL Server 中做的示例
SELECT CAST(CAST('2012-01-25 10:00:00.000' AS DATETIME) AS INT) + 2
结果是 40933
如何使用 c# 实现这一点?这个整数是从哪里来的,又是怎么来的?
参考:从 DateTime 转换为 INT但这是我在 c# 中需要的
我不明白为什么您需要DateTime
在 C# 中将 a 表示为整数,但您可以使用以下方法:
public static int DateTimeToInt(DateTime theDate)
{
return (int)(theDate.Date - new DateTime(1900, 1, 1)).TotalDays + 2;
}
您可以使用此方法进行反向操作:
public static DateTime IntToDateTime(int intDate)
{
return new DateTime(1900, 1, 1).AddDays(intDate - 2);
}
将 dateTime 转换为时间戳的扩展
public static int DateTimeToInt(this DateTime theDate)
{
int unixTime = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
return unixTime;
}
最好将 dateTime 转换为 long,DateTime.Ticks
为此使用属性