How to get Hours Difference of 2 Dates in C# and VB.NET

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

How to remove any post that is older than X days in WordPress

To remove any post that is older than X days in WordPress you can use the query below.

Sample mySQL

DELETE FROM wp_posts WHERE post_type = 'post' AND DATEDIFF(NOW(), post_date) > X

How to get monday date of current week in MSSQL

To get the Monday date of the current week you can use the snippet below.

SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) AS Monday

How to get the seconds between two dates in Python

To get the seconds between two dates in Python you can use the following snippet.

Sample Python

import datetime
import time

def DateDiffSeconds(dateOlder, dateNewer):
  timedelta = dateNewer - dateOlder
  return timedelta.days*24*3600 + timedelta.seconds

dateOlder = datetime.datetime(2014,07,01,00,00,00)
dateNewer = datetime.datetime(2014,07,02,11,30,00)
print DateDiffSeconds(dateOlder,dateNewer)

How to calculate the number of days in a month in C# and VB.NET

To calculate the number of days in a month in C# and VB.NET.

Sample C#

private static int DayCountPerMonth(DateTime date)
{
	if (date == null) {  throw new ArgumentNullException();}
	var calendar = new System.Globalization.GregorianCalendar();
	return calendar.GetDaysInMonth(date.Year, date.Month);
}

Sample VB.NET

Private Shared Function DayCountPerMonth(date As DateTime) As Integer
	If date Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim calendar = New System.Globalization.GregorianCalendar()
	Return calendar.GetDaysInMonth(date.Year, date.Month)
End Function