Visual Basic Code Snippet - Find starting and ending date for given date

Visual Basic Code Snippet - Find starting and ending date for given date

Visual Basic Code Snippet - Find starting and ending date for given date

(VB) Visual Basic code snippets return the Ending Date and Starting Date for given date. Simply pass DateTime value that need to find date and snippets will return starting and ending date as DateTime.

Bookmark:

Visual Basic Code Snippet - Find starting and ending date for given date

These .Net Visual Basic code snippets return the Ending Date and Starting Date for given date. Simply pass the DateTime value and these methods return the appropriate starting ending date values as a DateTime format.

Following code snippet returns ending date for given date.

''' <summary>
''' Get end date of given month
''' </summary>
''' <param name="_Date">Date to find end date</param>
''' <returns>DateTime of end date of the month</returns>
Public Function EndDateOfMonth(ByVal _Date As DateTime) As DateTime
    If _Date <> Nothing Then
        Return New DateTime(_Date.Year, _Date.Month, 1).AddMonths(1).AddDays(-1)
    Else
        Return _Date
    End If
End Function


Following code snippet returns starting date for given date.

''' <summary>
''' Get starting date of given month
''' </summary>
''' <param name="_Date">Date to find starting date</param>
''' <returns>DateTime of starting date of the month</returns>
Public Function StartDateOfMonth(ByVal _Date As DateTime) As DateTime
    If _Date <> Nothing Then
        Return New DateTime(_Date.Year, _Date.Month, 1)
    Else
        Return _Date
    End If
End Function


Here is a simple example showing how to use above functions (EndDateOfMonth / StartDateOfMonth) to get ending and starting date for given date.

Console.WriteLine("End date of the month : " & EndDateOfMonth(DateTime.Now).ToString())
Console.WriteLine("Start date of the month : " & StartDateOfMonth(DateTime.Now).ToString())


VB Keywords Used:

  • DateTime
  • DateTime.AddDays
  • DateTime.AddMonths

Code Snippet Information:

  • Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, DateTime, Starting date, Ending date, Date conversion, AddDays, AddMonths, DateTime Methods
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment