ASP Classic:对2个日期执行相同的减法,并在C#值中获得相同的值

时间:2019-03-19 10:20:15

标签: c# asp.net-mvc date asp-classic

我正在尝试从经典的ASP切换到ASP.Net MVC C#。在我的ASP Classic代码中,有一个部分会得到一个DateTime字段 A ,然后减去一个值 B 并得到并回答 14322.4290162037    问题是我不知道如何在 C#

中获得相同的答案值
 response.write "A: ==The Value: 19/03/2019 10:17:47==" & rstPASS("DATELASTUSE") & "<br>"
 response.write "B: ==The Value: 01/01/1980 === " & CDate("01/01/1980") & "<br>"
 response.write "A - B: I get: 14322.4290162037 " & rstBROPASS("DATELASTUSE") - CDate("01/01/1980") & "<br>"

我尝试过的事情

 idtLastUse = dt.Rows[0]["DATELASTUSE"] - DateTime.ParseExact(s: "01-01-1980", format: "dd/MM/yy", provider: null);

3 个答案:

答案 0 :(得分:3)

You can use:

DateTime d1 = new DateTime(2019, 3, 19, 10, 17, 47);
DateTime d2 = new DateTime(1980, 1, 1, 0, 0, 0);
Console.WriteLine(d1.ToOADate() - d2.ToOADate());

Look at the documentation for ToOADate to understand why.

答案 1 :(得分:0)

VB(所有口味)使用OLE Automation date格式以数字格式表示日期。

一个简短的描述应该是

  

浮点数,其整数部分是1899年12月30日午夜前后的天数,其小数部分表示该天的时间除以24。

我们可以使用一个简单的示例对此进行验证;

<%
Call Response.Write(FormatDateTime(CDate(0), vbLongDate))
%>

根据您的区域设置将返回结果;

30 December 1899

有用链接

答案 2 :(得分:-1)

您可以减去2个日期,如下所示:

DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddDays(5);

TimeSpan span = endTime.Subtract (startTime);
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );