.NET:日期时间转换为十进制转换为、日期、时间、NET

2023-09-05 04:31:48 作者:oo get out "

在SQL服务器(T-SQL),你可以一个DateTime变量转换为AA十进制值是这样的:

In SQL Server(T-SQL) you can convert a DateTime variable to a a decimal values like this:

CONVERT(DECIMAL(20,10),@mytime)
Sample Input: 2012-07-27 08:29:20.000
Sample Output: 41115.3537037037

有没有转换的日期时间在.NET(C#或VB)同类型小数的任何等同方法?

Is there any equivalent method of converting a DateTime in .NET(C# or VB) to the same type of decimal?

我期待在不同的日子比较倍。

I am looking to compare times on different days.

计算

41115.3537037037 % 1 = .3537037037

这可以让我轻松地在不同日期比较次。

This would allow me to easily compare times on different dates.

推荐答案

看起来,这是自1900年1月1日的天数。在这种情况下,你会使用:

It looks like this is "number of days since 1st January 1900". In which case, you'd use:

DateTime epoch = new DateTime(1900, 1, 1);
TimeSpan difference = date - epoch;
double days = difference.TotalDays;