Reputation: 61
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
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
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
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
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