Vince Ashby-Smith
Vince Ashby-Smith

Reputation: 1192

Generate days in month based on month

I'm wondering if anyone has an elegant solution for generating days based on a month selected for use in a dropdown. What i'm wanting to do is for the user to select the month, then populate a dropdown for days for that month. So for example if the user selected january you would have a dropdown list with values 1 - 31. The only bit i think might be tricky will be to do with leap years, so it may need to pass in year as well. Any ideas would be most appreciated :)

Upvotes: 1

Views: 1880

Answers (3)

moi_meme
moi_meme

Reputation: 9318

Enumerable.Range(1, System.DateTime.DaysInMonth(int year, int month));

Upvotes: 3

Roy Dictus
Roy Dictus

Reputation: 33139

You would need to create cascading dropdowns, where the user selects the year and the month and you generate a third dropdown based on that combination.

But a much easier solution would be to use a jQuery date picker: http://docs.jquery.com/UI/Datepicker

This does the hard work for you!

Upvotes: 2

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

Do this to populate your days dropdownlist:

System.DateTime.DaysInMonth(int year, int month);

Upvotes: 1

Related Questions