js 獲取日期及星期
阿新 • • 發佈:2018-11-19
獲取今天和昨天的日期及星期
//獲取日期 function getDaynum(num, str) { var today = new Date(); var nowTime = today.getTime(); var ms = 24*3600*1000*num; today.setTime(parseInt(nowTime + ms)); var oYear = today.getFullYear(); var oMoth = (today.getMonth() + 1).toString(); if (oMoth.length <= 1) oMoth = '0' + oMoth; var oDay = today.getDate().toString(); if (oDay.length <= 1) oDay = '0' + oDay; return oYear + str + oMoth + str + oDay; } //獲取星期幾 function getWeek(dateString){ var date; var dateArray = dateString.split("-"); date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]); return "星期" + "日一二三四五六".charAt(date.getDay()); }; //今天 var today = getWeek(getDaynum(-0, '-')); //昨天 var yestday = getWeek(getDaynum(-1, '-'));