Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:30:15+00:00 2026-05-31T12:30:15+00:00

I have a DateTimePicker which I have set to only show the month and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:30:16+00:00Added an answer on May 31, 2026 at 12:30 pm

    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 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a DateTimePicker which will be able to select the Time
I have a user control which has a DateTimePicker overlaid with the single line
i have DataTimePicker control in my form. i want to show only hh:mm how
I have a form which contains 2 buttons , a datetimepicker and datagridview which
I have following sql which is show previous days records from table. SELECT *
I have a Winform application with 2 DateTimePicker's on it, initialized as follows: private
I have a javascript datetimepicker that I downloaded online which I use to select
I have a DateTimePicker (nullable version) that I need to be read only. I'm
I have a DateTimePicker and it is currently displaying Friday, June 26 2009 How
I have created the DateTimePicker control. Now i want to add localization Support with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.