Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  180] [ 7]  / answers: 1 / hits: 39735  / 14 Years ago, thu, february 3, 2011, 12:00:00

I have a HTML page with 3 dropdowns for the month, day and year and I was wondering if there was a way to populate the month drop down properly depending on the month and year.



I haven't done this before on the client side, but it looks like a lot of controls like the jQuery DatePicker are doing that behind the scenes.


More From » date

 Answers
151

You can play with date objects:



var monthStart = new Date(year, month, 1);
var monthEnd = new Date(year, month + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24)


Arithmetic with Date objects gives a number of milliseconds.



This will even work for December; the Date constructor handles out-of-range arguments by wrapping around.



Note that month is zero-based (it must be between 0 and 11)


[#93925] Tuesday, February 1, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
4 Years ago
kevonmoisesf questions
Sat, Jan 23, 21, 00:00, 3 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
Wed, Jun 12, 19, 00:00, 5 Years ago
;