Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two DateTimes C#?

Tags:

date

c#

datediff

I need a function that can return the difference between the below two dates as 24.

DateTime a = new DateTime(2008, 01, 02, 06, 30, 00); DateTime b = new DateTime(2008, 01, 03, 06, 30, 00); 
like image 477
abmv Avatar asked May 10 '09 13:05

abmv


People also ask

How can I get the difference between two Datetimes in C#?

The difference between two dates can be calculated in C# by using the substraction operator - or the DateTime. Subtract() method. The following example demonstrates getting the time interval between two dates using the - operator.

What is the difference between DateTime and TimeSpan in C#?

The TimeSpan struct represents a duration of time, whereas DateTime represents a single point in time. Instances of TimeSpan can be expressed in seconds, minutes, hours, or days, and can be either negative or positive.

How do I use datediff in Python?

To get the difference between two-time, subtract time1 from time2. A result is a timedelta object. The timedelta represents a duration which is the difference between two-time to the microsecond resolution. To get a time difference in seconds, use the timedelta.


1 Answers

You can do the following:

TimeSpan duration = b - a; 

There's plenty of built in methods in the timespan class to do what you need, i.e.

duration.TotalSeconds duration.TotalMinutes 

More info can be found here.

like image 185
Joey Robert Avatar answered Sep 19 '22 04:09

Joey Robert