function getWeekInYear(date) {
//判断该星期是否跨年,如果跨年就是第一周
let weekStartDate = getWeekStartByDate(date) //一周开始时间
let endDate = getDateFromDay(weekStartDate, 6) //一周结束时间
if (weekStartDate.getFullYear() != endDate.getFullYear()) {
return endDate.getFullYear() + '年第1周'
let d1 = new Date(date)
let d2 = new Date(date)
d2.setMonth(0)
d2.setDate(1)
d2 = getWeekStartByDate(d2)
let rq = d1 - d2
let days = Math.ceil(rq / (24 * 60 * 60 * 1000)) + 1
let num = Math.ceil(days / 7)
return endDate.getFullYear() + '年第' + num + '周'
function getWeekStartByDate(date) {
let day = date.getDay()
return getDateFromDay(date, -day)
function getDateFromDay(dayDate, day) {
let date = new Date()
date.setTime(dayDate.getTime() + day * 24 * 60 * 60 * 1000)
return date
* 获取区间日期的小时/天/周/月数组支持跨年
*type:类型分为('hour'-小时,'day'-天,'week'-周,'month'-月)
*start:开始日期
*stop:结束日期
function getDuration(type,start,stop){
var weekArr = new Array();
var $array = new Array();
var current = new Date(start);
stop = new Date(stop);
while (current <= stop) {
$array.push( new Date (current) );
if(type == 'hour'){//小时
current.setHours(current.getHours() + 1);
}else if(type == 'day'){//天
current.setDate(current.getDate() + 1);
}else if(type == 'week'){//周
current.setDate(current.getDate() + 7);
}else if(type == 'month'){//月
current.setMonth(current.getMonth() + 1);
}else{//默认天
current.setDate(current.getDate() + 1);
for(var i=0;i<$array.length;i++){
let week = getWeekInYear($array[i]);
weekArr[i]=week;
return weekArr;
* 获取区间日期的季度
*start:开始日期
*stop:结束日期
function getFourSeason(start,stop) {
let starttime = new Date(start);
let endtime = new Date(stop);
let startYear = starttime.getFullYear();
let endYear = endtime.getFullYear();
let startMonth = starttime.getMonth() + 1;
let endMonth = endtime.getMonth() + 1;
var arr = [];
for (let i = startYear; i <= endYear; i++) {
// 开始年份与结束年份相同
if(startYear == endYear){
if(startMonth<=3 && endMonth<=3){
arr.push(
i+"年"+"第一季度"
}else if(startMonth<=3 && 3<endMonth && endMonth<=6){
arr.push(
i+"年"+"第一季度",
i+"年"+"第二季度"
}else if(startMonth<=3 && 6<endMonth && endMonth<=9){
arr.push(
i+"年"+"第一季度",
i+"年"+"第二季度",
i+"年"+"第三季度"
}else if(startMonth<=3 && 9<endMonth && endMonth<=12){
arr.push(
i+"年"+"第一季度",
i+"年"+"第二季度",
i+"年"+"第三季度",
i+"年"+"第四季度"
}else if(3<startMonth && startMonth<=6 && 3<endMonth && endMonth<=6){
arr.push(
i+"年"+"第二季度",
}else if(3<startMonth && startMonth<=6 && 6<endMonth && endMonth<=9){
arr.push(
i+"年"+"第二季度",
i+"年"+"第三季度"
}else if(3<startMonth && startMonth<=6 && 9<endMonth && endMonth<=12){
arr.push(
i+"年"+"第二季度",
i+"年"+"第三季度",
i+"年"+"第四季度"
}else if(6<startMonth && startMonth<=9 && 6<endMonth && endMonth<=9){
arr.push(
i+"年"+"第三季度"
}else if(6<startMonth && startMonth<=9 && 9<endMonth && endMonth<=12){
arr.push(
i+"年"+"第三季度",
i+"年"+"第四季度"
}else if(9<startMonth && startMonth<=12 && 9<endMonth && endMonth<=12){
arr.push(
i+"年"+"第四季度"
}else{
// 开始的那年
if (i == startYear) {
if(startMonth<=3){
arr.push(
startYear+"年"+"第一季度",
startYear+"年"+"第二季度",
startYear+"年"+"第三季度",
startYear+"年"+"第四季度"
}else if(startMonth>3&&startMonth<=6){
arr.push(
startYear+"年"+"第二季度",
startYear+"年"+"第三季度",
startYear+"年"+"第四季度"
}else if(startMonth>6&&startMonth<=9){
arr.push(
startYear+"年"+"第三季度",
startYear+"年"+"第四季度"
}else{
arr.push(
startYear+"年"+"第四季度"
if(i>startYear&&i<endYear){
arr.push(
i+"年"+"第一季度",
i+"年"+"第二季度",
i+"年"+"第三季度",
i+"年"+"第四季度"
// 结束的那年
if(i==endYear){
if(endMonth<=3){
arr.push(
endYear+"年"+"第一季度"
}else if(endMonth>3&&endMonth<=6){
arr.push(
endYear+"年"+"第一季度",
endYear+"年"+"第二季度"
}else if(endMonth>6&&endMonth<=9){
arr.push(
endYear+"年"+"第一季度",
endYear+"年"+"第二季度",
endYear+"年"+"第三季度"
}else{
arr.push(
endYear+"年"+"第一季度",
endYear+"年"+"第二季度",
endYear+"年"+"第三季度",
endYear+"年"+"第四季度"
return arr;
var now = new Date()
var year = now.getFullYear()
var month = now.getMonth() + 1 //0-11表示1-12月
var day = now.getDate()
var dateObj = {}
if (parseInt(month) < 10) {
month = '0' +
在我们在项目的时候,可能出现这样的一种情况,有一个开始时间和一个结束时间,而这两个时间用$('#StartTime').val(); 取出来的时候又是datetime 类型,我们需要求这个时间中的间隔天数这个时候我们应该怎么求呢?
var startDay =$("#Date").val();
var endDay =$("#EDate").val();
//间隔天数
var star...
今天一同事项目出了一个bug,她有两个时间,必须判断出这两个时间的属于哪周,并做出不同的逻辑处理
首先她自然想到了new Date().getTime()获取到了时间戳,然后new Date().getTime()/1000/60/60/24/7
然而里面的坑却慢慢的浮现出来。。。。
首先是getTime()拿到的时间戳,我很好奇的打印出来看了看
new Date('1970/1/1 00
monthArr.push(data.getFullYear() + '-' + (m.toString())) //注释掉上一句的结果就像这样:2023-3,2023-4....var str = Year.toString() + '-' + Month.toString()//简单粗暴的拼接,Year,Month是我选的年月。data.setMonth(data.getMonth() + 1, 1)// 获取到当前月份,设置月份。
initCalendar() {
const now = new Date();
const firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
const endDay = new Date(
now.getMonth() === 11
? new Date(now....
var start_date="20170212";
var end_date="20170619";
dateDiffs=DateDiff(start_date,end_date);
alert("天数:"+dateDiffs);
//根据起始日期和结束日期计算天数
function DateDiff(sDate1, sDate2){
主要参考这篇文章:https://blog.csdn.net/u012976879/article/details/88718620?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromM
// console.log(val);
var month = new Date(val).getMonth() + 1;
var years = new Date(val).getFullYear();
var days = new Date(years, month, 0);
var s = days.getDate()...
JS文件: function planCycon(){ var selectObj=document.getElementById("planCyc"); var index=selectObj.selectedIndex var va=selectObj.options[index].value; if(va=="year"){ year(); }else if(va=="week")
我们要把起始时间的前面的日期取出来和截至日期的后面的日期取出来进行格式化,获取到起止时间
Firstyear = this.defaultStValue.substr(0, 4);
Firstmonth = this.defaultStValue.substr(4, 2);
Lastyear = this.defaultEtValue.substr(7, 4);
var choosedate = new Date(); //选择当前日期
var firstday = new Date();
var years= choosedate .getFullYear();
firstday.setFullYear(years);
firstday.setMonth( 0 ); //1月
firstday.setDate( 1 ); //1日
var dayssecond = choosedate - firstday;.