Date.getDay() javascript returns wrong day
嗨,我是javascript的新手
我有这样的javascript代码
1 2 3 4 5 6 7 8 9 10 11 12 | 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; } |
例如:
当我打电话给
当我打电话给
使用
The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
来自MDN关于getDay:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDay
Returns the day of the week for the specified date according to local
time.
你可能想要getDate:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDate
Returns the day of the month for the specified date according to local
time.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getDay
我遇到了类似的问题。
像这样:
1 2 3 4 5 6 7 8 9 10 11 | 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()]; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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) } |
并使用此代码调用
1 2 3 4 | date="Fri Aug 26 2016 18:06:01 GMT+0530 (India Standard Time)" formatDate(date,function(result){ console.log('Date=',result); }); |