How can I automatically set the datetimepicker value to the first month of the month?
You can try this:
dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1)
Date.AddDays
will add the specified number of days to the specified date
Now.Day
will give the current day
dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1)
will always point to the first day of the current month
Updates: set the date to 01 of the selected month per month.
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
DateTimePicker1.Value = CDate("01/" & DateTimePicker1.Value.Month & "/" & DateTimePicker1.Value.Year)
End Sub
Just use the DateTime Constructor to create a new date value using 1 as the day of the month and set the datetime element to that value:
dtpDate.Value = New DateTime(dtpDate.Value.Year, dtpDate.Value.Month, 1)
source to share
I found the answer very helpful and very easy. I have provided the link below.
Getting the first and last day of the current month
DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
Originally Posted by Marcin Juraszek ( https://stackoverflow.com/users/1163867/marcinjuraszek )
source to share