获取今天,昨天,本周,上周,本月,上月时间
编程学习 2021-07-04 19:20www.dzhlxh.cn编程入门
本篇文章主要分享了获取今天,昨天,本周,上周,本月,上月时间实例代码,具有一定的参考价值,狼蚁网站SEO优化跟着长沙网络推广一起来看下吧
话不多说,请看代码:
//获取今天 var nowDate= new Date(); //当天日期 console.log(nowDate); //今天是本周的第几天 var nowDayOfWeek= nowDate.getDay(); console.log(nowDayOfWeek); //当前日 var nowDay = nowDate.getDate(); console.log(nowDay); //当前月 var nowMonth = nowDate.getMonth(); console.log(nowMonth); //当前年 var nowYear = nowDate.getFullYear(); console.log(nowYear); //var nowHours = nowDate.getHours(); //var nowMinutes = nowDate.getMinutes(); //var nowSeconds = nowDate.getSeconds(); nowYear += (nowYear < 2000) ? 1900 : 0; // console.log(nowYear); var lastMonthDate = new Date(); //上月日期 console.log(lastMonthDate); lastMonthDate.setDate(1); console.log(lastMonthDate.setDate(1)); lastMonthDate.setMonth(lastMonthDate.getMonth()-1); console.log(lastMonthDate.setMonth(lastMonthDate.getMonth()-1)); var lastYear = lastMonthDate.getYear(); console.log(lastYear); var lastMonth = lastMonthDate.getMonth(); console.log(lastMonth); //格式化日期:yyyy-MM-dd function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth()+1; var myweekday = date.getDate(); //var myHours = date.getHours(); //var myMinutes = date.getMinutes(); //var mySeconds = date.getSeconds(); if(mymonth < 10){ mymonth = "0" + mymonth; } if(myweekday < 10){ myweekday = "0" + myweekday; } //if(myHours < 10){ // myHours = "0" + myHours; /