C# DateTime Difference

C# DateTime Difference

How to get Difference between two dates? Lets see how to do it using [C#] and Vb.net.
We will use Time Span to get difference.

[C#] program to get Difference between two dates

C# DateTime Difference

C#.net program uses Time Span to get difference between From date and To Date.

using System;
namespace DateAndTime.Examples
{
class Program
{
static void Main(string[] args)
{
DateTime fromdate = DateTime.Parse("12/31/2011");
DateTime todate = DateTime.Parse("01/02/2012");
TimeSpan difference = todate.Subtract(fromdate);
Console.WriteLine(difference);
Console.ReadLine();
}
}
}
VB.net-Program


VB.net program to get difference between two dates

VB.net example of DateTime difference
Namespace DateAndTime.Examples
Class Program
Private Shared Sub Main(args As String())
Dim fromdate As DateTime = DateTime.Parse("12/31/2011")
Dim todate As DateTime = DateTime.Parse("01/02/2012")
Dim difference As TimeSpan = todate.Subtract(fromdate)
Console.WriteLine(difference)
Console.ReadLine()
End Sub
End Class
End Namespace
Output

2.00:00:00 You can format this output using string formatters or Time Span formatters in C#.net or VB.net.
As There is only 2 day difference in given input dates, program output shows 2.

C#.net,VB.netDateTime Differance in days,hours,minutes and seconds

Using an instance of TimeSpan obtained by substracting two days, we can get differance in days, hours, minutes and seconds.

(C#.net) datetime differance example
using System;
namespace DateAndTime.Examples
{
class Program
{
static void Main(string[] args)
{
DateTime fromdate = DateTime.Parse("12/31/2011 12:10:15");
DateTime todate = DateTime.Parse("01/02/2012 10:03:13");
TimeSpan differance = todate.Subtract(fromdate);
Console.WriteLine("Total Time Span: {0} ", differance);
Console.WriteLine("Number of Days: {0} days.", differance.Days);
Console.WriteLine("Hours {0} ", differance.Hours);
Console.WriteLine("Minutes: {0}", differance.Minutes);
Console.WriteLine("Seconds: {0}", differance.Seconds);
Console.ReadLine();
}
}
}
(VB.net) DAteTime differance example
Namespace DateAndTime.Examples
Class Program
Private Shared Sub Main(args As String())
Dim fromdate As DateTime = DateTime.Parse("12/31/2011 12:10:15")
Dim todate As DateTime = DateTime.Parse("01/02/2012 10:03:13")
Dim differance As TimeSpan = todate.Subtract(fromdate)
Console.WriteLine("Total Time Span: {0} ", differance)
Console.WriteLine("Number of Days: {0} days.", differance.Days)
Console.WriteLine("Hours {0} ", differance.Hours)
Console.WriteLine("Minutes: {0}", differance.Minutes)
Console.WriteLine("Seconds: {0}", differance.Seconds)
Console.ReadLine()
End Sub
End Class
End Namespace
Total Time Span: 1.21:52:58
Number of Days: 1 days.
Hours 21
Minutes: 52
Seconds: 58

DateTime examples in C#.net and VB.net

How to format Time in C#.net and VB.net program.
DateTime Time Format string {HH:mm:ss}.
C#.net date time Culture.
C#.net DateTime Month.
string was not recognized as a valid date time
Get Today
DateTime in English UK format
Get Day Of year
DateTime.Today vs. DateTime.Now
DateTime Parse Exact method.
Parse Exact DateTime

Tags:

DateTimeTryParse using C# and VB.net exampleC# Parse method of DateTimeDateTime.Today Vs DateTime.NowC# DateTimeC# DateTime ToString()IFormatProvider DateTime C#.netDateTimeTryParse with IFormatProvider culture information using c#C# Convert String To DateTimeDateTime in C# current Date and TimeC# Datetime MonthAsp.net Cache Object

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.