.NET:日期时间转换为十进制 [英] .NET: Convert datetime to decimal

查看:749
本文介绍了.NET:日期时间转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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;

这篇关于.NET:日期时间转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆