根据已知日期取日期 星期
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> </head> <body> </body> <script> var a='2020-02-28';//选择的日志 const today = new Date(a)//以选择的日期 实例化一个时间对象 var days=new Array(); var Y = today.getFullYear() ;//年 var M = today.getMonth()+1; //月 if(M<10){ M='0'+M; } var D = today.getDate() ;//日 if(D<10){ D='0'+D; } days[0] = Y+'-'+M+'-'+D; //放入数组 for(var i=1;i<=14;i++){ today.setDate(today.getDate() + 1) //加上相应的天数 Y = today.getFullYear() ;//年 M = today.getMonth()+1; //月 if(M<10){ M='0'+M; } D = today.getDate() ;//日 if(D<10){ D='0'+D; } days[i] = Y+'-'+M+'-'+D; //放入数组 } alert(days); console.log(days); console.log(a); var weekarry=[]; getWeek(days); function isNull(object){ if(object == null || typeof object == "undefined"){ return true; } return false; }; function getWeek(dateString){ var date; if(isNull(dateString)){ date = new Date(); }else{ for(let i=0;i<days.length;i++){ var dateArray = dateString[i].split("-"); // console.log(dateArray); date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]); weekarry.push("星期" + "日一二三四五六".charAt(date.getDay())); } alert(weekarry); } //var weeks = new Array("日", "一", "二", "三", "四", "五", "六"); //return "星期" + weeks[date.getDay()]; }; </script> </html>