封装的JS文件, 适用与绝大多数的时间计算(可以阅读看一下)
Date.prototype.Format = function (fmt) {
//需要JS格式化时间,后期做的时候方便使用
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
S: this.getMilliseconds(), //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
return fmt;
};
// 通过年获取 本年周数 year 年
function getWeekNum(year) {
const firstDay = new Date(parseInt(year), 0, 1).getDay(), // 设置该年1.1星期
endDay = new Date(parseInt(year), 11, 31).getDay(); // 设置该年12.31星期
// firstDay 和endDay 为0的时候 ,让其为7
if (!firstDay) {
firstDay = 7;
}else if(!endDay) {endDay = 7;}
//该年最大周次.1.1.在周一到周四,则为该年第一周,否则为上年最后一周.
const flag =
firstDay >= 1 &&
firstDay <= 4 &&
(endDay == 7 || endDay == 4 || endDay == 5 || endDay == 6);
// 1.1.和12.31.都在本年,则该年有53周,否则52周
const maxWeek = flag ? 53 : 52;
return maxWeek;
}
//通过年份、周数获取日期范围 year 年 weeks 周 getWeekRange(2019,12))--> 2019-03-18~2019-03-24
function getWeekRange(year, weekNum) {
const yearStart = new Date(parseInt(year), 0, 1), //设置该年1.1.
firstDay = yearStart.getDay(); // 星期
// 对1.1.所在周的前后几天特殊处理.
const other = firstDay >= 0 && firstDay <= 4 ? firstDay - 1 : firstDay - 8;
//时间调整,得出要计算周的起/始时间.
// 考虑距离当年1.1.的总天数
let days = (parseInt(weekNum, 10) - 1) * 7 - other;
// 一天时间的毫秒数
let oneDay = 24 * 60 * 60 * 1000;
// 当前周 第一天 离1/1/70的毫秒数.
let dateInMs = oneDay * days + yearStart.getTime();
//日期调整(设置1/1/70至今的毫秒数)
const weekStart = new Date(dateInMs);
//当前所选周最后一天处理,同上.
let dateInMsE = oneDay * 6 + dateInMs;
const weekEnd = new Date(dateInMsE);
let weekdays = [];
for (let i = 0; i < 7; i++) {
let n = i;
let weekd = oneDay * n + dateInMs;
let weekDate = new Date(weekd);
let m = weekDate.getMonth() + 1;
m = m < 10 ? "0" + m : m;
let d = weekDate.getDate();
d = d < 10 ? "0" + d : d;
weekdays.push(m + "." + d);
}
//月和日的处理,一位变两位,如:1->01.
let month = weekStart.getMonth() + 1;
month = month < 10 ? "0" + month : month;
let day = weekStart.getDate();
day = day < 10 ? "0" + day : day;
let monthE = weekEnd.getMonth() + 1;
monthE = monthE < 10 ? "0" + monthE : monthE;
let dayE = weekEnd.getDate();
dayE = dayE < 10 ? "0" + dayE : dayE;
return [month + "月" + day + "日-" + (monthE + "月" + dayE + "日"), weekdays];
}
// 根据具体日期,获取当天所在周的周数
function getYearWeek(dateString) {
var da = dateString; //日期格式2015-12-30
//当前日期
var date1 = new Date(
da.substring(0, 4),
parseInt(da.substring(5, 7)) - 1,
da.substring(8, 10)
);
//1月1号
var date2 = new Date(da.substring(0, 4), 0, 1);
//获取1月1号星期(以周一为第一天,0周一~6周日)
var dateWeekNum = date2.getDay() - 1;
if (dateWeekNum < 0) {
dateWeekNum = 6;
}
if (dateWeekNum < 4) {
//前移日期
date2.setDate(date2.getDate() - dateWeekNum);
} else {
//后移日期
date2.setDate(date2.getDate() + 7 - dateWeekNum);
}
var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
if (d < 0) {
var date3 = date1.getFullYear() - 1 + "-12-31";
return getYearWeek(date3);
} else {
//得到年数周数
var year = date1.getFullYear();
var week = Math.ceil((d + 1) / 7);
return week;
}
}
// 获取每年 所有周的范围
function getAllWeekRange(year) {
let weekNum = getWeekNum(year);
let weeks = [];
for (let index = 1; index <= weekNum; index++) {
let range = getWeekRange(year, index);
weeks.push(range);
}
return weeks;
}
function getWeekDayAll(starDay, endDay) {
var arr = [];
var dates = [];
// 设置两个日期UTC时间
var db = new Date(starDay);
var de = new Date(endDay);
// 获取两个日期GTM时间
var s = db.getTime() - 24 * 60 * 60 * 1000;
var d = de.getTime() - 24 * 60 * 60 * 1000;
// 获取到两个日期之间的每一天的毫秒数
for (var i = s; i <= d; ) {
i = i + 24 * 60 * 60 * 1000;
arr.push(parseInt(i));
}
// 获取每一天的时间 YY-MM-DD
for (var j in arr) {
var time = new Date(arr[j]);
var year = time.getFullYear(time);
var mouth =
time.getMonth() + 1 >= 10
? time.getMonth() + 1
: "0" + (time.getMonth() + 1);
var day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate();
var YYMMDD = year + "-" + mouth + "-" + day;
dates.push(YYMMDD);
}
return dates;
}
// var dataAll = getDayAll("2020-01-01", "2020-02-01");
// console.log(dataAll);
//通过具体日期获得该日期所在的周数
function GetWeekByDate(testTime) {
var arrayMonthDay = null;
// 处理IOS的时间格式问题
testTime = testTime.replace(/-/g, "/");
var nowDate = new Date(testTime); // 当前日期
var nowYear = nowDate.getFullYear(); // 当前年
var nowMonth = 1; // 第一月
var nowDay = 1; // 第一天
// 根据该年有多少周来让周数为1 为2019单独处理,日历只显示到2019年1月1号
if(getWeekNum(nowYear) == 53){
var week = 1;
}else if(nowYear == 2019 && getWeekNum(nowYear) == 52){
var week = 1;
}else{
var week = 0;
} // 第一周 // 根据年是否为闰年,得到 arrayMonthDay的值
if (nowYear % 400 == 0 || (nowYear % 4 == 0 && nowYear % 100 != 0)) {
arrayMonthDay = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
} else {
arrayMonthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
} // 获取当前年的一月一号 是星期几
var time = nowYear + "/" + nowMonth + "/" + nowDay;
var weekDay = new Date(time).getDay();
// 这里的getDay的范围是0~6 的需要处理一下,让0 变为7,方便让其是周一的时候再加一
if (!weekDay) {
weekDay = 7;
}
var bdate = new Date(testTime); // 选择的时间
var byear = bdate.getFullYear(); // 当前年
var bmonth = bdate.getMonth() + 1; // 月
var bday = bdate.getDate(); // 日期
var bweek = 0;
var isyear = true;
while (isyear) {
// 如果当前的月份是验证的月份的值,并且当前的天数是验证的日期的值,则结束循环,保存日期
if (nowMonth == bmonth && nowDay == bday) {
bweek = week;
isyear = false;
break;
} // 否则当天++,星期xx
nowDay++;
weekDay++; // 如果当天的值>当前月份所对应的的最大天数,则月份+1,日期改为1
if (nowDay > arrayMonthDay[nowMonth - 1]) {
nowMonth++;
if (nowMonth > 12) {
nowMonth = 1;
}
nowDay = 1;
} // 如果当前星期大于7,则说明这周已完成,周数+1,星期改为 1
if (weekDay > 7) {
week++;
weekDay = 1;
}
}
var value = bweek;
return value;
}
module.exports = {
getYearWeek: getYearWeek,
getWeekRange: getWeekRange,
getWeekNum: getWeekNum,
getAllWeekRange: getAllWeekRange,
GetWeekByDate: GetWeekByDate,
getWeekDayAll: getWeekDayAll,
};