Date.getDay()JavaScript返回错误的一天

嗨,我新的JavaScript我有这样的JavaScript代码

alert(DATE.value); var d = new Date(DATE.value); var year = d.getFullYear(); var month = d.getMonth(); var day = d.getDay(); alert(month); alert(day); if(2012 < year < 1971 | 1 > month+1 > 12 | 0 >day > 31){ alert(errorDate); DATE.focus(); return false; } 

DATE.value =“11/11/1991”

当我打电话警报(一天); 它显示我3;
当我打电话警报(四); 这是返回我正确的信息

使用.getDate而不是.getDay

getDay返回的值是与星期几相对应的整数:星期日为0,星期一为1,星期二为2,等等。

从关于getDay的MDN: https : //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDay

根据当地时间返回指定date的星期几。

你可能想getDate: https : //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDate

根据当地时间返回指定date的月份的当天。

getDay()返回星期几。 但是,您可以使用getDate()方法。

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDay

getDay()会给你一个星期的一天。 您正在寻找getDate()

我有一个类似的问题。 date.getMonth()返回一个从0 to 11的索引。 一月是0 。 如果你创build一个新的date()并且你想获得有关date而不是当前date的信息,你只需要减less1个月。

像这样

 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()]; } 
 function formatDate(date, callback) { var weekday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); var day = weekday[date.getDay()]; console.log('day',day); var d = date.getDate(); var hours = date.getHours(); ampmSwitch = (hours > 12) ? "PM" : "AM"; if (hours > 12) { hours -= 12; } else if (hours === 0) { hours = 12; } var m = date.getMinutes(); var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var month = months[date.getMonth()]; var year = date.getFullYear(); newdate = day + ', ' + month + ' ' + d + ',' + year + ' at ' + hours + ":" + m + " " + ampmSwitch callback(newdate) } 

并用此代码调用

 date="Fri Aug 26 2016 18:06:01 GMT+0530 (India Standard Time)" formatDate(date,function(result){ console.log('Date=',result); });