How to find difference fron 2 dates
Loading
How to find difference fron 2 dates
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jul 14, 2023, 1:18 AM EDT
DateTime compareTo = DateTime.Parse("07/08/2021 05:05:00 AM"); DateTime now = DateTime.Now; var dateSpan = DateTimeSpan.CompareDates(compareTo, now); Console.WriteLine("Date diffrence between dt:" + compareTo + " and dt:" + now); Console.WriteLine("Years: " + dateSpan.Years); Console.WriteLine("Months: " + dateSpan.Months); Console.WriteLine("Days: " + dateSpan.Days); Console.WriteLine("Hours: " + dateSpan.Hours); Console.WriteLine("Minutes: " + dateSpan.Minutes); Console.WriteLine("Seconds: " + dateSpan.Seconds); Console.WriteLine("Milliseconds: " + dateSpan.Milliseconds);Lokesh VarmanRank #374 · 6.8K reputationPosted Jul 13, 2023, 1:08 PM EDT
DateTime date1 = new DateTime(2023, 7, 1); DateTime date2 = new DateTime(2023, 7, 10); TimeSpan difference = date2 - date1; Console.WriteLine("The difference is: " + difference.Days + " days.");Hope this helps !
Prasad RaveendranRank #320 · 7.8K reputationPosted Jul 13, 2023, 9:33 PM EDT
The
DateTime.Subtractmethod is available in all versions of C#. It is a method provided by theDateTimestructure in the .NET Framework, and it has been available since the earliest versions of .NET.You can use the
DateTime.Subtractmethod to calculate the difference between two dates in any version of C# without any compatibility issues.DateTime date1 = new DateTime(2023, 6, 1); DateTime date2 = new DateTime(2023, 7, 13); TimeSpan difference = date2.Subtract(date1); Console.WriteLine("The difference is: " + difference.TotalDays + " days.");