Indrah
Indrah

Reputation: 61

How to set datetimepicker value to 1st of selected month automatically?

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..

Upvotes: 3

Views: 12964

Answers (4)

Nabeel Hassan
Nabeel Hassan

Reputation: 3

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)

Upvotes: 0

Manoj Yadwad
Manoj Yadwad

Reputation: 31

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)

Upvotes: 1

Matt Wilko
Matt Wilko

Reputation: 27322

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)

Upvotes: 3

user3972104
user3972104

Reputation:

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

Upvotes: 0

Related Questions