Find all the days in a month with Date object?

后端 未结 6 1014
Happy的楠姐
Happy的楠姐 2020-11-30 01:07

is there a way to have all the days of a month or of a year? I am looking for this in order to disable some specific days in a datepicker, i would have a page in the back-of

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 02:05

    Here's a loopp that runs through each month to determine the last day of that month. Javascript Date object indexes months starting at zero and if you set the day to zero it reverts back to last day of prior month. Handy for determining leap year last day for February

    Date( 2012, 12, 0) will return Dec 31, 2012

    Date (2012,0,0) will return Dec 31,2011

    and the all important one to figure out is February with

    Date ( 2012,3,0) Returns Feb 29 since leap year this year

    var mos=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
    
    for (i = 0; i < 12; i++) {
        var lastDate = new Date(2012, i+1, 0);
        $('body').append('Last day of ' + mos[i] + ' is ' + lastDate.getDate()+'
    ') }

    DEMO: http://jsfiddle.net/5k8sn/1/

提交回复
热议问题