To get Hours Difference of 2 Dates in C# and VB.NET you can use the following snippet.

Sample C#

/// <summary>
/// Returns the Hour Difference of two Datetimes as Double Value.
/// </summary>
/// <param name="dateOne">The original date.</param>
/// <param name="dateTwo">The Datei to be Checked against the Original Date.</param>
/// <returns></returns>
public static Double DifferenceInHours(this DateTime dateOne, DateTime dateTwo)
{
	return (dateOne - dateTwo).TotalHours;
}

Sample VB.NET

''' <summary>
''' Returns the Hour Difference of two Datetimes as Double Value.
''' </summary>
''' <param name="dateOne">The original date.</param>
''' <param name="dateTwo">The Datei to be Checked against the Original Date.</param>
''' <returns></returns>
<System.Runtime.CompilerServices.Extension> _
Public Shared Function DifferenceInHours(dateOne As DateTime, dateTwo As DateTime) As [Double]
	Return (dateOne - dateTwo).TotalHours
End Function

As always, this extension method has been added to the Github Fesslersoft.Extensions Repository

3 thought on “How to get Hours Difference of 2 Dates in C# and VB.NET”

Leave a Reply