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

前端 未结 8 1864
一个人的身影
一个人的身影 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:38

    I am using this approach in my current project and found that I needed correct for round off errors. So instead of using monthLength in my code, I had to use this instead:

    monthLength.toFixed(0)
    

    For example if I have an object in which I am storing a text date field, it may look like this:

    obj.month = theMonths[mm - 1] + " " + monthLength.toFixed(0) + ", " + obj.year;
    
    0 讨论(0)
  • 2020-12-05 12:39

    Copy from another post: Get number days in a specified month using javascript?

    //Month is 1 based
    function daysInMonth(month,year) {
    return new Date(year, month, 0).getDate();
    }
    
    //July
    daysInMonth(7,2009); //31
    //February
    daysInMonth(2,2009); //28
    daysInMonth(2,2008); //29
    

    All the credits to @c_harm, really great solution

    0 讨论(0)
提交回复
热议问题