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

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

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

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)

How to check if string is date in C# and VB.NET

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

Sample C#

public bool IsDate(string input)
{
	DateTime result;
	return DateTime.TryParse(input, out result);
}

Sample VB.NET

Public Function IsDate(input As String) As Boolean
	Dim result As DateTime
	Return DateTime.TryParse(input, result)
End Function

How to change the time in a DateTime in c# or VB.NET

To change the time in a DateTime you can either use Timespan or the add methods of the DateTime class. I will show you both ways and give you also a extension method.

 

using the Timespan class

Sample C#

DateTime today = DateTime.Now;
TimeSpan newTime = new TimeSpan(13, 37, 0);
today = today.Date + newTime;

Sample VB.NET

Dim today As DateTime = DateTime.Now
Dim newTime As New TimeSpan(13, 37, 0)
today = today.Date + newTime

using the Add methods of the DateTime class

Sample C#

DateTime today = DateTime.Now;
today = today.Date.AddHours(13).AddMinutes(37).AddSeconds(0);

Sample VB.NET

Dim today As DateTime = DateTime.Now
today = today.Date.AddHours(13).AddMinutes(37).AddSeconds(0)

The Extension Method

Sample C#

public static DateTime ChangeTime(this DateTime dateTime, int hours, int minutes, int seconds, int milliseconds)
{
      return new DateTime(dateTime.Year,dateTime.Month,dateTime.Day,hours,minutes,seconds,milliseconds,dateTime.Kind);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ChangeTime(dateTime As DateTime, hours As Integer, minutes As Integer, seconds As Integer, milliseconds As Integer) As DateTime
    Return New DateTime(dateTime.Year, dateTime.Month, dateTime.Day, hours, minutes, seconds, milliseconds, dateTime.Kind)
End Function

for more informations take a look at the MSDN: DateTime Structure, TimeSpan Structure, Extension Methods (C# Programming Guide)

How to calculate a person’s age using C# and VB.NET

This code snippet can help you to calculate a Person’s age using C# and VB.NET.

C# Snippet

VB.NET Snippet

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links: DateTime Structure, DateTime Methods

Compatibility:
working .NET 2.0
working .NET 3.0
not tested .NET 3.5
not working .NET 4.0
not working .NET 4.5