Date.getDay() javascript returns wrong day

后端 未结 6 1095
挽巷
挽巷 2020-11-30 23:09

Hi I\'m new in javascript I have such javascript code

alert(DATE.value);
var d = new Date(DATE.value);
var year = d.getFullYear();
var month = d.getMonth();
         


        
6条回答
  •  盖世英雄少女心
    2020-11-30 23:45

    I had a similar problem. date.getMonth() returns an index ranging from 0 to 11. January is 0. If you create a new date()-object and you want to get information about a costum date not the current one you have to decrease only the month by 1.

    Like this:

    function getDayName () {
    var year = 2016;
    var month = 4;
    var day = 11;
    
    var date = new Date(year, month-1, day);
    var weekday = new Array("sunday", "monday", "tuesday", "wednesday",
                        "thursday", "friday", "saturday");
    
    return weekday[date.getDay()];
    }
    

提交回复
热议问题