Getting the difference of two instances of DateTime in milliseconds

What would be the right way to do this? I am using ASP.NET MVC 3.

+5
source share
5 answers
        DateTime a = ...
        DateTime b = ...
        var ms = a.Subtract(b).TotalMilliseconds;
+11
source
(datetime2 - datetime1).TotalMilliseconds
+6
source

I think this should work. Since you asked for it, I assume you don't know which one is a later date :)

Math.Abs((date1 - date2).TotalMilliseconds)
+4
source

Subtraction will be my choice ...

DateTime earlier = DateTime.Now;
// ...
DateTime later = DateTime.Now;
double result = (later - earlier).TotalMilliseconds;
0
source
    public static Int64 GetDifferencesBetweenTwoDate(DateTime newDate, DateTime oldDate, string type)
    {
        var span = newDate - oldDate;
        switch (type)
        {
            case "tt": return (int)span.Ticks;
            case "ms": return (int)span.TotalMilliseconds;
            case "ss": return (int)span.TotalSeconds;
            case "mm": return (int)span.TotalMinutes;
            case "hh": return (int)span.TotalHours;
            case "dd": return (int)span.TotalDays;
        }
        return 0;
    }
0
source

All Articles