How do I subtract two DateTime values from another DateTime value and have the result saved to a double?
            Asked
            
        
        
            Active
            
        
            Viewed 9.8k times
        
    36
            
            
        - 
                    7The result in what unit of time? Ticks? Seconds? Any example input/output? – BoltClock Mar 03 '11 at 05:27
- 
                    What's this what should be the the output after subtracting – Developer Mar 03 '11 at 05:28
- 
                    Related post - [Showing Difference between two datetime values in hours](https://stackoverflow.com/q/4946316/465053) – RBT Feb 22 '19 at 08:56
7 Answers
44
            In .NET, if you subtract one DateTime object from another, you will get a TimeSpan object. You can then use the Ticks property on that TimeSpan object to get the number of ticks between the two DateTime objects. However, the ticks will be represented by a Long, not a Double.
DateTime date1;
DateTime date2;
Long diffTicks = (date2 - date1).Ticks;
There are other interesting properties on the TimeSpan object like TotalMilliseconds and TotalMinutes and things like that which can help you out, and may be more what you are looking for.
 
    
    
        Brandon Montgomery
        
- 6,924
- 3
- 48
- 71
26
            
            
        DateTime startTime = DateTime.Now; 
DateTime endTime = DateTime.Now.AddSeconds( 75 );  
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 );
 
    
    
        Chris Taylor
        
- 52,623
- 10
- 78
- 89
 
    
    
        sajoshi
        
- 2,733
- 1
- 18
- 22
4
            
            
        I think this is what you need.
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.UtcNow;
var result = d1 - d2;
double dResult = result.Ticks;
 
    
    
        Razor
        
- 17,271
- 25
- 91
- 138
2
            
            
        Use DateTime.Subtract which will return TimeSpan , then use TotalSeconds property of the result which is of type double.
 
    
    
        Nikhil Vaghela
        
- 920
- 2
- 11
- 36
1
            
            
        var Date1 = new DateTime(2018, 12, 15);
var Date2 = new DateTime(2019, 1, 1);
TimeSpan result1 = Date2.Subtract(Date1);
Console.WriteLine(result1);
//To calculate difference between two dates use TotalDays() method
double result2 = Date2.Subtract(Date1).TotalDays;
Console.WriteLine(result2);
//Output:
17.00:00:00
17
Note:
- Date2 should be greater than Date1 or else the method will return a negative value
- Subtract() method returns value of type TimeSpan while TotalDays() method returns value of type double
 
    
    
        Tahir77667
        
- 2,290
- 20
- 16
0
            
            
        You should try in this.
DateTime prevDate = DateTime.Parse("25-Feb-2011 12:30");
double subDouble = DateTime.Now.Ticks - prevDate.Ticks;
 
    
    
        Waqas Raja
        
- 10,802
- 4
- 33
- 38
0
            
            
        I am not sure what is you want to store if you need a double
  double difference = date2.ToOADate() - date1.ToOADate();
 
    
    
        V4Vendetta
        
- 37,194
- 9
- 78
- 82
 
    