3

when i check monthwise radiobtn the datetimepicker value should automatically set to 1st day of selected month.

how to do this??

tried

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

but not working as expected..

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Indrah
  • 61
  • 1
  • 2
  • 10

4 Answers4

3

Just use the DateTime Constructor to create a new date value using 1 as the day of the month and set the datetime picker to that value:

dtpDate.Value = New DateTime(dtpDate.Value.Year, dtpDate.Value.Month, 1)
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
1

I have found the answer very useful and very easy. I have provided the link below.

Getting 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

You can try this :

 dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1)

Date.AddDays will add specified number of days to the date mentioned

Now.Day will give the current day

dtpDate.Value = Now.Date.AddDays(-(Now.Day) + 1) will always points to the first day of current month

Updates: set date to 01 of selected month on month change

   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
  • I want like this. If I select MARCH , DTPVALUE should go to 1st of MARCH. Like that i need to set. is it possible? – Indrah Oct 30 '14 at 05:44
  • is there any way to extract date,month part from datetimepicker using vb.net. so that i can set the date – Indrah Oct 30 '14 at 06:01
  • Your update to select the first of the month on a date change is not locale friendly so won't work on locales using MM/dd/yyyy – Matt Wilko Jan 07 '16 at 08:39
0

You can change manually date, month and Year of the Selected DateTime in C# by using this I m changing the Selected Date only and set Selected Date 1st Of the selected moth year...

 var moodifiedDate = new DateTime(input.Year, input.Month, 1)