[removed] calculate number of days in month for a given year

前端 未结 8 1891
一个人的身影
一个人的身影 2020-12-05 12:12

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 ye

8条回答
  •  不思量自难忘°
    2020-12-05 12:32

    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)

提交回复
热议问题