js获取本周、上周、本⽉、上⽉、本季度、上季度的开始
结束⽇期
js 获取本周、上周、本⽉、上⽉、本季度、上季度的开始结束⽇期
/**
* 获取本周、本季度、本⽉、上⽉的开始⽇期、结束⽇期
*/
var now = new Date(); //当前⽇期
var nowDayOfWeek = Day(); //今天本周的第⼏天
var nowDay = Date(); //当前⽇
var nowMonth = Month(); //当前⽉
var nowYear = Year(); //当前年
js当前日期加一天nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //上⽉⽇期
lastMonthDate.setDate(1);
lastMonthDate.Month() - 1);
var lastYear = Year();
var lastMonth = Month();
//格式化⽇期:yyyy-MM-dd
function formatDate(date) {
var myyear = FullYear();
var mymonth = Month() + 1;
var myweekday = Date();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday);
}
//获得某⽉的天数
function getMonthDays(myMonth) {
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
}
//获得本季度的开始⽉份
function getQuarterStartMonth() {
var quarterStartMonth = 0;
if (nowMonth < 3) {
quarterStartMonth = 0;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 3;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 6;
}
if (nowMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
}
//获得本周的开始⽇期
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}
//获得本周的结束⽇期
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}
//获得上周的开始⽇期
function getLastWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
return formatDate(weekStartDate);
}
//获得上周的结束⽇期
function getLastWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
return formatDate(weekEndDate);
}
//获得本⽉的开始⽇期
function getMonthStartDate() {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
//获得本⽉的结束⽇期
function getMonthEndDate() {
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}
//获得上⽉开始时间
function getLastMonthStartDate() {
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}
//获得上⽉结束时间
function getLastMonthEndDate() {
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}
//获得本季度的开始⽇期
function getQuarterStartDate() {
var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartDate);
}
/
/或的本季度的结束⽇期
function getQuarterEndDate() {
var quarterEndMonth = getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear, quarterEndMonth,
getMonthDays(quarterEndMonth));
return formatDate(quarterStartDate);
}
js 计算⽉/周的第⼀天和最后⼀天
因为项⽬开发中遇到需要向后台传本周的开始和结束时间,以及上⼀周的起⽌时间,就琢磨了半天,总算写出来⼀套,写篇⽂章是为了⽅便⾃⼰记忆,也是分享给需要的⼈,⽔平有限,写的不好请见谅:
getDateStr3函数是为了把时间对象转变为yy-mm-dd的字符串,⽅便传值;
getWeekStartAndEnd函数是获取周的起⽌时间,并且⽤getDateStr3转换成字符串放到数组中,其中参数0代表当前周,-1代表前⼀周,-2代表上上周,以此类推,反过来也可以1代表下⼀周;
getMonthStartAndEnd函数是获取⽉的起⽌时间,传参同上
//获取当前⽇期yy-mm-dd
//date 为时间对象
function getDateStr3(date) {
var year = "";
var month = "";
var day = "";
var now = date;
year = ""+FullYear();
if((Month()+1)<10){
month = "0"+(Month()+1);
}else{
month = ""+(Month()+1);
}
if((Date())<10){
day = "0"+(Date());
}else{
day = ""+(Date());
}
return year+"-"+month+"-"+day;
}
/
**
* 获得相对当前周AddWeekCount个周的起⽌⽇期
* AddWeekCount为0代表当前周为-1代表上⼀个周为1代表下⼀个周以此类推
* **/
function getWeekStartAndEnd(AddWeekCount) {
//起⽌⽇期数组
var startStop = new Array();
//⼀天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//获取当前时间
var currentDate = new Date();
/
/相对于当前⽇期AddWeekCount个周的⽇期
currentDate = new Time() + (millisecond * 7*AddWeekCount));
//返回date是⼀周中的某⼀天
var week = Day();
//返回date是⼀个⽉中的某⼀天
var month = Date();
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//获得当前周的第⼀天
var currentWeekFirstDay = new Time() - (millisecond * minusDay)); //获得当前周的最后⼀天
var currentWeekLastDay = new Time() + (millisecond * 6)); //添加⾄数组
startStop.push(getDateStr3(currentWeekFirstDay));
startStop.push(getDateStr3(currentWeekLastDay));
return startStop;
}
/**
* 获得相对当⽉AddMonthCount个⽉的起⽌⽇期
* AddMonthCount为0 代表当⽉为-1代表上⼀个⽉为1代表下⼀个⽉以此类推
* ***/
function getMonthStartAndEnd(AddMonthCount) {
//起⽌⽇期数组
var startStop = new Array();
//获取当前时间
var currentDate = new Date();
var Month()+AddMonthCount;
if(month<0){
var n = parseInt((-month)/12);
month += n*12;
currentDate.FullYear()-n);
}
currentDate = new Date(currentDate.setMonth(month));
//获得当前⽉份0-11
var currentMonth = Month();
//获得当前年份4位年
var currentYear = FullYear();
//获得上⼀个⽉的第⼀天
var currentMonthFirstDay = new Date(currentYear, currentMonth,1);
//获得上⼀⽉的最后⼀天
var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0);
//添加⾄数组
startStop.push(getDateStr3(currentMonthFirstDay));
startStop.push(getDateStr3(currentMonthLastDay));
//返回
return startStop;
}
好了这篇⽂章就介绍到这了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论