获取本周本年本月时间
程序员文章站
2022-06-15 22:46:27
...
function getCurrentdate() {
var startStop = new Array();
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getFullYear();
var firstDay = new Date(currentYear, currentMonth, 1);
if (currentMonth == 11) {
currentYear++;
currentMonth = 0;
} else {
currentMonth++;
}
var week = currentDate.getDay();
var month = currentDate.getDate();
var minusDay = week != 0 ? week - 1 : 6;
var millisecond = 1000 * 60 * 60 * 24;
var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
var monday = new Date(new Date(currentDate.toLocaleDateString()).getTime() - (minusDay *
millisecond));
var sunday = new Date(new Date(monday.toLocaleDateString()).getTime() + (6 * millisecond));
var currentYearFirstDate = new Date(currentYear, 0, 1);
var currentYearLastDate = new Date(currentYear, 11, 31);
switch (nowTime) {
case 'year':
startStop.push(currentYearFirstDate);
startStop.push(currentYearLastDate);
break;
case 'month':
startStop.push(firstDay);
startStop.push(lastDay);
break;
default:
startStop.push(monday);
startStop.push(sunday);
break;
}
return startStop;
};