Reputation: 125
I want to decrement the day in DateTimePicker
when button is clicked and keep the customized time
at 10:00:00
.
When I use the following code and when going back from month to another (previous month on the same year) it works fine.
but the problem comes when I try to go back from 2016 to 2015. For example when I try to go back from 1-1-2016
to 31-12-2015
it gives error: "Year, Month, and Day parameters describe an un-representable DateTime"
dateTimePicker1.Value = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day - 1, 10, 00, 0);
Anyone knows how can I decrease the day and keep customized time when going back from year to another (previous year)? please help. Thank you
Upvotes: 0
Views: 255
Reputation: 35679
dateTimePicker1.Value
is a DateTime
and DateTime
has methods to modify dates and times correctly
dateTimePicker1.Value = dateTimePicker1.Value.Date.AddDays(-1).AddHours(10);
It takes current date [midnight], calculates previous date [still midnight] and adds 10 hours [morning]
Upvotes: 1