在js中如何获取上一个季度的开始、结束日期
2018-03-23 14:03
686 查看
Date.prototype.format =function(format)
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4- RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var dayMSec = 24 * 3600 * 1000;
var today = new Date();
//得到今天距离本周一的天数
function getDayBetweenMonday(){
//得到今天的星期数(0-6),星期日为0
var weekday = today.getDay();
//周日
if(weekday == 0){
return 6;
}else{
return weekday - 1;
}
}
function getLastDay(){
var yestodayMSec=today.getTime() -dayMSec;
var yestoday = new Date(yestodayMSec);
return yestoday.format('yyyy-MM-dd')+","+yestoday.format('yyyy-MM-dd');
}
function getLastWeek(){
//得到距离本周一的天数
var weekdayBetween = getDayBetweenMonday();
//得到本周星期一的毫秒值
var nowMondayMSec = today.getTime() - weekdayBetween * dayMSec;
//得到上周一的毫秒值
var lastMondayMSec = nowMondayMSec - 7 * dayMSec;
//得到上周日的毫秒值
var lastSundayMSec = nowMondayMSec - 1 * dayMSec;
var lastMonday = new Date(lastMondayMSec);
var lastSunday = new Date(lastSundayMSec);
return lastMonday.format('yyyy-MM-dd')+","+lastSunday.format('yyyy-MM-dd');
}
function getLastMonth(){
//得到上一个月的第一天
var lastMonthFirstDay = new Date(today.getFullYear() , today.getMonth()-1 , 1);
//得到本月第一天
var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1);
//得到上一个月的最后一天的毫秒值
var lastMonthLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec;
var lastMonthLastDay = new Date(lastMonthLastDayMSec);
return lastMonthFirstDay.format('yyyy-MM-dd')+","+lastMonthLastDay.format('yyyy-MM-dd');
}
function getLastQuarter(){
//得到上一个季度的第一天
var lastQuarterFirstDay = new Date(today.getFullYear() , today.getMonth() - 3 , 1);
//得到本月第一天
var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1);
//得到上一个季度的最后一天的毫秒值
var lastQuarterLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec;
var lastQuarterLastDay = new Date(lastQuarterLastDayMSec);
return lastQuarterFirstDay.format('yyyy-MM-dd') +","+lastQuarterLastDay.format('yyyy-MM-dd');
}
常说的季度规则。
调用getLastQuarter();就行了
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4- RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var dayMSec = 24 * 3600 * 1000;
var today = new Date();
//得到今天距离本周一的天数
function getDayBetweenMonday(){
//得到今天的星期数(0-6),星期日为0
var weekday = today.getDay();
//周日
if(weekday == 0){
return 6;
}else{
return weekday - 1;
}
}
function getLastDay(){
var yestodayMSec=today.getTime() -dayMSec;
var yestoday = new Date(yestodayMSec);
return yestoday.format('yyyy-MM-dd')+","+yestoday.format('yyyy-MM-dd');
}
function getLastWeek(){
//得到距离本周一的天数
var weekdayBetween = getDayBetweenMonday();
//得到本周星期一的毫秒值
var nowMondayMSec = today.getTime() - weekdayBetween * dayMSec;
//得到上周一的毫秒值
var lastMondayMSec = nowMondayMSec - 7 * dayMSec;
//得到上周日的毫秒值
var lastSundayMSec = nowMondayMSec - 1 * dayMSec;
var lastMonday = new Date(lastMondayMSec);
var lastSunday = new Date(lastSundayMSec);
return lastMonday.format('yyyy-MM-dd')+","+lastSunday.format('yyyy-MM-dd');
}
function getLastMonth(){
//得到上一个月的第一天
var lastMonthFirstDay = new Date(today.getFullYear() , today.getMonth()-1 , 1);
//得到本月第一天
var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1);
//得到上一个月的最后一天的毫秒值
var lastMonthLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec;
var lastMonthLastDay = new Date(lastMonthLastDayMSec);
return lastMonthFirstDay.format('yyyy-MM-dd')+","+lastMonthLastDay.format('yyyy-MM-dd');
}
function getLastQuarter(){
//得到上一个季度的第一天
var lastQuarterFirstDay = new Date(today.getFullYear() , today.getMonth() - 3 , 1);
//得到本月第一天
var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1);
//得到上一个季度的最后一天的毫秒值
var lastQuarterLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec;
var lastQuarterLastDay = new Date(lastQuarterLastDayMSec);
return lastQuarterFirstDay.format('yyyy-MM-dd') +","+lastQuarterLastDay.format('yyyy-MM-dd');
}
常说的季度规则。
调用getLastQuarter();就行了
相关文章推荐
- 使用moment.js获取一个自然周开始日期和结束日期
- JS获取本周、本季度、本月、上月的开始日期、结束日期
- js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期
- js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期
- JS获取本周、本季度、本月、上月的开始日期、结束日期
- js获取上周、本周、上月、本月、上季度、本季度的开始日期、结束日期(无bug)
- JS获取本周、本季度、本月、上月的开始日期、结束日期
- 在sql语句中,如何获取一个日期的周开始日期,周结束日期
- js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期
- js获取某周、某月、下月、某季度的开始日期、结束日期及判断日期第几周
- js获取某周、某月、下月、某季度的开始日期、结束日期及判断日期第几周
- js获取一天的开始时间和结束时间和日期与字符串、数值之间的转换
- js获得本周,本月,本季度的开始日期和结束日期
- js获得本周,本月,本季度的开始日期和结束日期
- 获取本周、本季度、本月、上月的开始日期、结束日期
- C# 下面的代码示例演示如何结束异步操作以获取请求的流,然后开始一个请求以获取响应。....
- JS获取及验证开始结束日期的方法
- js根据日期获取本周的开始日期和结束日期
- js获得本周,本月,本季度的开始日期和结束日期
- js 获取昨天上周 上个月 上个季度的开始时间和结束时间