.NET: convert datetime to decimal

In SQL Server (T-SQL), you can convert the DateTime variable to decimal values ​​as follows:

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

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

I am looking for a comparison of time on different days.

Calculation

 41115.3537037037 % 1 = .3537037037 

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

+4
source share
2 answers

This seems to be the "number of days since January 1, 1900." In this case you should use:

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

I know that you indicated that you want to compare the time of day (get the decimal part), but since the title is how to convert DateTime to Decimal, here's how to get an integer numeric value in one (integer and decimal parts). You can do the following (VB.NET) and it will be equivalent to SQL Server:

 Dim TimeZero As New DateTime(1900, 1, 1) Dim MyTime As DateTime = DateTime.Parse("2012-07-27 08:29:20.000") Dim DecResult As Decimal = DateDiff(DateInterval.Day, TimeZero, MyTime) + (MyTime.TimeOfDay.TotalSeconds / (24*3600)) 
0
source

All Articles