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

Please rate this snippet

How to disable comments on posts older than date X in WordPress

To disable comments on posts older than date X in WordPress you can use the snippet below.

Sample mySQL

UPDATE wp_posts SET comment_status = 'closed' WHERE post_date < '2013-01-01' AND post_status = 'publish'

Please rate this snippet

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

Please rate this snippet

How to get the Month name of a Datetime in C# and VB.NET

To get the Month name of a Datetime in C# and VB.NET you can use the following snippet.

Sample C#

Console.WriteLine(new DateTime(2010, 1, 1).ToString("MMM", CultureInfo.InvariantCulture)); //Jan
Console.WriteLine(new DateTime(2010, 1, 1).ToString("MMMM", CultureInfo.InvariantCulture)); //January

Sample VB.NET

Console.WriteLine(New DateTime(2010, 1, 1).ToString("MMM", CultureInfo.InvariantCulture))
'Jan
Console.WriteLine(New DateTime(2010, 1, 1).ToString("MMMM", CultureInfo.InvariantCulture))
'January

Please rate this snippet

How to check if Date is Workday in C# and VB.NET

To check if Date is Workday in C# and VB.NET you can use the following snippet.

Sample C#

public static bool IsWorkDay(DateTime date, bool isWorkDaySaturday)
{
	if (date == null) {  throw new ArgumentNullException("date");}
	switch (date.DayOfWeek)
	{
		case DayOfWeek.Saturday:
			return isWorkDaySaturday;
		case DayOfWeek.Sunday:
			return false;
		default:
			return true;
	}
}

Sample VB.NET

Public Shared Function IsWorkDay(date As DateTime, isWorkDaySaturday As Boolean) As Boolean
	If date Is Nothing Then
		Throw New ArgumentNullException("date")
	End If
	Select Case date.DayOfWeek
		Case DayOfWeek.Saturday
			Return isWorkDaySaturday
		Case DayOfWeek.Sunday
			Return False
		Case Else
			Return True
	End Select
End Function

Please rate this snippet

How to get the BuildDate of a Assembly in C# and VB.NET

Sample C#

public DateTime GetBuildDate(Assembly assembly)
{
	var location = assembly.Location;
	const int headerOffset = 60;
	const int linkerTimestampOffset = 8;
	var buffer = new byte[2048];
	Stream stream = null;

	try
	{
		stream = new FileStream(location, FileMode.Open, FileAccess.Read);
		stream.Read(buffer, 0, 2048);
	}
	finally
	{
		if (stream != null)
		{
			stream.Close();
		}
	}

	var i = BitConverter.ToInt32(buffer, headerOffset);
	var secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset);
	var dt = new DateTime(1970, 1, 1, 0, 0, 0);
	dt = dt.AddSeconds(secondsSince1970);
	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
	return dt;
}

Sample VB.NET

Public Function GetBuildDate(assembly as Assembly) As DateTime
	Dim location = assembly.Location
	Const  headerOffset As Integer = 60
	Const  linkerTimestampOffset As Integer = 8
	Dim buffer = New Byte(2047) {}
	Dim stream As Stream = Nothing

	Try
		stream = New FileStream(location, FileMode.Open, FileAccess.Read)
		stream.Read(buffer, 0, 2048)
	Finally
		If stream IsNot Nothing Then
			stream.Close()
		End If
	End Try

	Dim i = BitConverter.ToInt32(buffer, headerOffset)
	Dim secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset)
	Dim dt = New DateTime(1970, 1, 1, 0, 0, 0)
	dt = dt.AddSeconds(secondsSince1970)
	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours)
	Return dt
End Function

Please rate this snippet

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)

Please rate this snippet

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

Please rate this snippet

How to select date from datetime in MSSQL

To select date from datetime in MSSQL you can use the following snippet.

Sample MSSQL

SELECT CONVERT(VARCHAR(10),Testdatecolumn,104) FROM dbo.tblTest

The 104 is for tt.mm.jjjj, a list of formats can be found at the MSDN CAST and CONVERT (Transact-SQL)

Please rate this snippet