Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Calculate minutes between two dates in C#
Calculating the difference in minutes between two dates is a common requirement in C# applications. The DateTime structure and TimeSpan class provide built-in functionality to perform this calculation efficiently.
Syntax
Following is the basic syntax for calculating minutes between two dates −
DateTime date1 = new DateTime(year, month, day, hour, minute, second); DateTime date2 = new DateTime(year, month, day, hour, minute, second); TimeSpan difference = date2 - date1; double minutes = difference.TotalMinutes;
How It Works
When you subtract one DateTime from another, C# returns a TimeSpan object that represents the time difference. The TotalMinutes property converts this difference into minutes as a double value.
Using DateTime Constructor
The most common approach is to create DateTime objects using the constructor and then subtract them −
using System;
public class Demo {
public static void Main() {
DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);
DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);
TimeSpan ts = date2 - date1;
Console.WriteLine("No. of Minutes (Difference) = {0}", ts.TotalMinutes);
}
}
The output of the above code is −
No. of Minutes (Difference) = 47699.0833333333
Using DateTime.Parse() Method
You can also parse date strings to calculate the difference −
using System;
public class Demo {
public static void Main() {
DateTime startDate = DateTime.Parse("2023-01-15 10:30:00");
DateTime endDate = DateTime.Parse("2023-01-15 14:45:30");
TimeSpan difference = endDate - startDate;
double totalMinutes = difference.TotalMinutes;
Console.WriteLine("Start Time: " + startDate);
Console.WriteLine("End Time: " + endDate);
Console.WriteLine("Difference in Minutes: " + totalMinutes);
}
}
The output of the above code is −
Start Time: 1/15/2023 10:30:00 AM End Time: 1/15/2023 2:45:30 PM Difference in Minutes: 255.5
Using DateTime.Now for Current Time
To calculate minutes from a specific date to the current time −
using System;
public class Demo {
public static void Main() {
DateTime pastDate = new DateTime(2023, 12, 1, 9, 0, 0);
DateTime currentDate = new DateTime(2023, 12, 1, 17, 30, 0); // Simulating current time
TimeSpan timeDifference = currentDate - pastDate;
double minutesPassed = timeDifference.TotalMinutes;
Console.WriteLine("Past Date: " + pastDate);
Console.WriteLine("Current Date: " + currentDate);
Console.WriteLine("Minutes Elapsed: " + minutesPassed);
Console.WriteLine("Hours Elapsed: " + timeDifference.TotalHours);
}
}
The output of the above code is −
Past Date: 12/1/2023 9:00:00 AM Current Date: 12/1/2023 5:30:00 PM Minutes Elapsed: 510 Hours Elapsed: 8.5
Return Value
The TotalMinutes property returns a double value representing the total number of minutes in the time span. It can include fractional minutes if the difference includes seconds.
Conclusion
Calculating minutes between two dates in C# is straightforward using DateTime subtraction and the TotalMinutes property of TimeSpan. This approach works with any two DateTime objects, whether created from constructors, parsed from strings, or using DateTime.Now.
