本文主要是介绍JavaScript 得到上周时间范围,本月时间范围,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//1. 得到本月一号为开始时间,得到当前时间减两天为结束时间
function SetDayDate() {//当前时间减两天var dataValue = new Date(new Date().getTime() - (1000 * 60 * 60 * 48));//(1000*60*60*24)是1千毫秒(是1秒)乘60(是1分)乘60(是1小时)*24(是一天)加减一天把前边的-+换一下就行。var year = dataValue.getFullYear();var month = dataValue.getMonth() + 1;var day = dataValue.getDate();if (month < 10) {month = '0' + month;}if (day < 10) {if (day < 1) {day = '01';} else {day = '0' + day;}}var stime = year + '/' + month + '/' + '01'; //开始时间var etime = year + '/' + month + '/' + day; //结束时间
}//2. 得到当前时间的上周时间范围
function SetWeekDate() {var dataValue = new Date();var year = dataValue.getFullYear();var month = dataValue.getMonth() + 1;var day = dataValue.getDate();var thisWeekStart; //本周周一的时间if (dataValue.getDay() == 0) { //周天的情况;thisWeekStart = (new Date(year + '/' + month + '/' + day)).getTime() - ((dataValue.getDay()) + 6) * 86400000;} else {thisWeekStart = (new Date(year + '/' + month + '/' + day)).getTime() - ((dataValue.getDay()) - 1) * 86400000;}//获得上周时间var prevWeekStart = thisWeekStart - 7 * 86400000;//上周周一的时间var prevWeekEnd = thisWeekStart - 1 * 86400000;//上周周日的时间var stime = formatDate(new Date(prevWeekStart)); //开始时间var etime = formatDate(new Date(prevWeekEnd)); //结束时间}//3. 得到当前时间的上月时间范围
function SetMonthDate() {var dataValue = new Date();var currentYear = dataValue.getFullYear();var currentMonth = dataValue.getMonth();var prevCurrentYear = 0, prevCurrentMonth = 0;if (currentMonth == 0) {prevCurrentYear = currentYear - 1;prevCurrentMonth = 12;} else {prevCurrentYear = currentYear;prevCurrentMonth = currentMonth - 1;}var prevmonthLastday = (new Date(currentYear, currentMonth, 1)).getTime() - 86400000;var stime = formatDate(new Date(prevCurrentYear, prevCurrentMonth, 1)); //开始时间var etime = formatDate(new Date(prevmonthLastday)); //结束时间
}//格式化日期:yyyy/MM/dd
function formatDate(date) {var myyear = date.getFullYear();var mymonth = date.getMonth() + 1;var myweekday = date.getDate();if (mymonth < 10) {mymonth = "0" + mymonth;}if (myweekday < 10) {myweekday = "0" + myweekday;}return (myyear + "/" + mymonth + "/" + myweekday);
}
这篇关于JavaScript 得到上周时间范围,本月时间范围的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!