Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a date time picker always contain the last day of the month while showing month and year only?

I have a DateTimePicker which I have set to only show the month and year as follows:

myDateTimePicker.Format = DateTimePickerFormat.Custom;
myDateTimePicker.CustomFormat = "MMMM yyyy";
myDateTimePicker.ShowUpDown = true;

However, I want the value of the date to always be the last day of the selected month, so I set the DateTime in the ValueChanged event using:

DateTime selectedDate = myDateTimePicker.Value;
DateTime lastDayOfMonth = new DateTime(
    selectedDate.Year,
    selectedDate.Month,
    DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month));
myDateTimePicker.Value = lastDayOfMonth;

The problem is, if I have a month like March selected, and I change the month to February using the up/down controls, I get the following error before I can handle the ValueChanged event:

ArgumentOutOfRangeException was unhandled
Year, Month, and Day parameters describe an un-representable DateTime.

This is understandable because the date was 31 March and it is being changed to 31 February which is an invalid date. However, I want to change it to 28 February (or 29 February).

How can I achieve this?

like image 559
Warren Blumenow Avatar asked Mar 14 '12 13:03

Warren Blumenow


People also ask

How do you only show month in date picker?

This can be done by setting "display:none" to ". ui-datepicker-calendar" CSS class.

How do I format a date picker in Excel?

Click the Data tab. In the Data type box, click Date and Time (dateTime). Click Format. In the Date and Time Format dialog box, in the Display the time like this list, click the option that you want, and then click OK.

What is the method of time and date picker?

Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components. In this tutorial, we are going to demonstrate the use of Date Picker through DatePickerDialog.


2 Answers

Very strange, I tried to reproduce your problem, and when I switch from March to February, my control just doesn't display anything... Maybe we have different framework versions, who handle the same error differently.

Anyway, one solution would be to set the datetime picker to the first of each month, and when you need the value, you can just use your code as it currently is:

DateTime lastDayOfMonth = new DateTime(
    selectedDate.Year, 
    selectedDate.Month, 
    DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month)); 

Since you never use the day value of your dattime picker, you can just set it to 1, which will always give an existing date.

This solution leaves me with a bad feeling somehow, since you are using a date that differs from the date you get from your control - always a possible source of errors, IMO. Remember to put comments into your code, explaining why you are doing it this way ;-)

like image 119
Treb Avatar answered Oct 20 '22 03:10

Treb


I'd probably use the 1st of the month like Treb Selected, but extend DateTimePicker so that when you did this:

MyDateTimePicker.Value

it would do something like this:

get{
return value.addMonths(1).addDays(-1)
}
like image 38
Ian Jacobs Avatar answered Oct 20 '22 03:10

Ian Jacobs