Search code examples
c#asp.net-mvcdateasp-classic

ASP Classic: perform same subtraction of 2 dates and get the same value in C# value


I am trying to switch from classic ASP to ASP.Net MVC C#. There is a part where my ASP Classic code will get a DateTime Field A then will subtract a value B and will get the and answer 14322.4290162037 The thing is I don't know how to get the same answer value in 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>"

What I have tried

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

Solution

  • 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.