Solutions to calculate difference between two date time
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace firstCSharpDemo
{
class Program
{
public static void Main(string[] args)
{
string formats = "MM/dd/yyyy HH:mm:ss";
Console.WriteLine("Enter First Date and Time in MM/dd/yyyy HH:mm:ss format: ");
// DateTime startTime = DateTime.Parse(Console.ReadLine());
DateTime startTime = DateTime.ParseExact(Console.ReadLine(),formats, System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine("Enter Second Date and Time in MM/dd/yyyy HH:mm:ss format: ");
DateTime endTime = DateTime.ParseExact(Console.ReadLine(), formats, System.Globalization.CultureInfo.InvariantCulture);
TimeSpan span = endTime.Subtract(startTime);
int months = (int)(span.Days / 30.436875);
int Year = (int)(span.Days / 365.2425);
Console.WriteLine("Difference in days : " + span.Days);
Console.WriteLine("Difference in Months: " + months);
Console.WriteLine("Difference in Years : " + Year);
Console.WriteLine("Difference in minutes: " + span.Minutes);
Console.WriteLine("Difference in hours: " + span.Hours);
Console.WriteLine("Difference in seconds : " + span.Seconds);
Console.ReadKey();
}
}
}