With C# or VB.net, you can be coding for getting the start date and end date of the month.

The start date is by default the 1st of the month, but you can customize and limit your personal start date by yourself.

The end date of the month does not default like the start date, it bases on some of the months. So it may be difficult for getting the end date of the month when you have a lot of generation dates from VB.net or C#.

So this article can help you with how to generate the date of the month to you, follow step by step :

Getting current month

Current month, when you’re staying in this month and you want to get the start date and end date. For example, use the Now property for getting a current date.

VB.net

Dim Now As DateTime = DateTime.Now
Dim Dates = New DateTime(Now.Year, Now.Month, 1)
Dim startDate = Dates
Dim endDate = Dates.AddMonths(+1).AddDays(-1)
txtstart_date.Text = Format(startDate, "yyyy-MM-dd")
txtend_date.Text = Format(endDate, "yyyy-MM-dd")

C#

DateTime Now = DateTime.Now;
var Dates = new DateTime(Now.Year, Now.Month, 1);
var startDate = Dates;
var endDate = Dates.AddMonths(+1).AddDays(-1);
txtstart_date.Text = Strings.Format(startDate, "yyyy-MM-dd");
txtend_date.Text = Strings.Format(endDate, "yyyy-MM-dd");

Getting previous month

The previous month, when you’re finding the start date and end date of the previous month, for example, use the Now property for getting the current date and calculate to find the previous month.

VB.net

Dim Now As DateTime = DateTime.Now
Dim Dates = New DateTime(Now.Year, Now.Month, 1)
Dim startDate = Dates.AddMonths(-1)
Dim endDate = Dates.AddDays(-1)
txtstart_date.Text = Format(startDate, "yyyy-MM-dd")
txtend_date.Text = Format(endDate, "yyyy-MM-dd")

C#

DateTime Now = DateTime.Now;
var Dates = new DateTime(Now.Year, Now.Month, 1);
var startDate = Dates.AddMonths(-1);
var endDate = Dates.AddDays(-1);
txtstart_date.Text = Strings.Format(startDate, "yyyy-MM-dd");
txtend_date.Text = Strings.Format(endDate, "yyyy-MM-dd");

Getting next month

The next month, it’s the same as the previous month based on the Now property for calculating and finding the next month.

VB.net

Dim Now As DateTime = DateTime.Now
Dim Dates = New DateTime(Now.Year, Now.Month, 1)
Dim startDate = Dates.AddMonths(+1)
Dim endDate = Dates.AddMonths(+2).AddDays(-1)
txtstart_date.Text = Format(startDate, "yyyy-MM-dd")
txtend_date.Text = Format(endDate, "yyyy-MM-dd")

C#

DateTime Now = DateTime.Now;
var Dates = new DateTime(Now.Year, Now.Month, 1);
var startDate = Dates.AddMonths(+1);
var endDate = Dates.AddMonths(+2).AddDays(-1);
txtstart_date.Text = Strings.Format(startDate, "yyyy-MM-dd");
txtend_date.Text = Strings.Format(endDate, "yyyy-MM-dd");