Getting the difference of two DateTime instances in milliseconds

13,709

Solution 1

        DateTime a = ...
        DateTime b = ...
        var ms = a.Subtract(b).TotalMilliseconds;

Solution 2

(datetime2 - datetime1).TotalMilliseconds

Solution 3

I'm thinking this should work. Since you asked for foolproof, I'm assuming you don't know which of the two is the later date :)

Math.Abs((date1 - date2).TotalMilliseconds)
Share:
13,709
aliadly
Author by

aliadly

I am a ASP.net/PHP web developer. Recently I have been involved with a Compact Framework project which explains my multiple CF related questions.

Updated on June 05, 2022

Comments

  • aliadly
    aliadly almost 2 years

    What would be a proper fool-proof way of doing so? I am using ASP.NET MVC 3.