How can I automatically set the datetimepicker value to the first month of the month?

when i check the monthly radiopicker the datetimepicker should automatically set to the 1st day of the selected month.

how to do it?

tried

dtpDate.value=new Date(1:MM:YYYY)

      

but doesn't work as expected.

+3


source to share


3 answers


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

      

0


source


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)

      

+2


source


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 )

0


source







All Articles