Calculate minutes between two dates in C#
To calculate the minutes between 2 dates we can simply define aTimeSpan
object which is going to be equal to first DateTime
minus second DateTime
objects, finally we convert TimeSpan object to minutes number by calling property TotalMinutes
.Example
using System;
public class Program
{
public static void Main()
{
DateTime date1 = new DateTime(2022, 7, 15, 08, 15, 20);
DateTime date2 = new DateTime(2022, 8, 17, 11, 14, 25);
TimeSpan ts = date2 - date1;
Console.WriteLine("No. of Minutes = {0}", ts.TotalMinutes);
}
}
Output
No. of Minutes = 47699.0833333333