日期总是会在很多地方用到,快速获取,分享好用的时间插件啊,快速获取近几天,近几个月等常用日期
如果只是用某一个方法,也可以自己写啊,大多数的计算都是根据时间戳计算
1.格式化日期或获取当前日期
/*
* time 时间戳
* isAddZero 是否添0
* dateSplit 日期分隔符
* timeSplit 时间分隔符
* format 格式 Y-year M-month D-day h-hour m-minute s-sencond,选择项,默认显示年月日,时分秒
*/
function toGetDayDate(time = "", isAddZero = true, format = "YYYYMMDD hhmmss", dateSplit = "-", timeSplit = ":") {
let date = time ? new Date(time) : new Date;
let formatDate = ''
let Y = date.getFullYear();
let M = isAddZero ? addZreo(date.getMonth() + 1) : date.getMonth() + 1;
let D = isAddZero ? addZreo(date.getDate()) : date.getDate();
let h = isAddZero ? addZreo(date.getHours()) : date.getHours();
let m = isAddZero ? addZreo(date.getMinutes()) : date.getMinutes();
let s = isAddZero ? addZreo(date.getSeconds()) : date.getSeconds();
function addZreo(num) {
return num > 9 ? num : '0' + num
}
// return `${Y}${dateSplit}${M}${dateSplit}${D} ${h}${timeSplit}${m}${timeSplit}${s}`
switch (format) {
case 'YYYYMMDD':
formatDate = `${Y}${dateSplit}${M}${dateSplit}${D}`
break;
case 'YYYYMMDD hh':
formatDate = `${Y}${dateSplit}${M}${dateSplit}${D} ${h}`
break;
case 'YYYYMMDD hhmm':
formatDate = `${Y}${dateSplit}${M}${dateSplit}${D} ${h}${timeSplit}${m}`
break;
default:
formatDate = `${Y}${dateSplit}${M}${dateSplit}${D} ${h}${timeSplit}${m}${timeSplit}${s}`
break;
}
return formatDate
}
2.获取昨日
function getYesterday() {
let time = (new Date()).valueOf() - 60*60*24*1000;
return toGetDayDate(time, true, 'YYYYMMDD')
}
3.获取明天
function getTomorrow() {
let time = (new Date()).valueOf() + 60*60*24*1000;
return toGetDayDate(time, true, 'YYYYMMDD')
}
4.获取本周
function getWeeks() {
let timeStamp = (new Date()).valueOf();
let day = new Date().getDay(); //获得今天是周几
let tempArr = [];
for (let i = 0; i < 7; i++) {
tempArr.push( toGetDayDate(new Date(timeStamp + (i - (day + 6) % 7)*1000*24*60*60), 'YYYYMMDD'))
}
return tempArr;
}
5.获取上周
function getLastWeeks() {
let day = new Date().getDay(); //获得今天是周几
let timeStamp = (new Date()).valueOf() - 7*1000*24*60*60;
let tempArr = [];
for (let i = 0; i < 7; i++) {
tempArr.push( toGetDayDate(new Date(timeStamp + (i - (day + 6) % 7)*1000*24*60*60), 'YYYYMMDD'))
}
return tempArr;
}
6.获取下周
function getNextWeeks() {
let day = new Date().getDay(); //获得今天是周几
let timeStamp = (new Date()).valueOf() + 7*1000*24*60*60;
let tempArr = [];
for (let i = 0; i < 7; i++) {
tempArr.push( toGetDayDate(new Date(timeStamp + (i - (day + 6) % 7)*1000*24*60*60), 'YYYYMMDD'))
}
return tempArr;
}
7.获取本月
function getMonth() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var min = new Date(year, month, 1);
var max = new Date(year, month + 1, 0);
return [toGetDayDate(min, 'YYYYMMDD'), toGetDayDate(max, 'YYYYMMDD')]
}
8.获取上月
function getLastMonth() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var min = new Date(year, month - 1, 1);
var max = new Date(year, month, 0);
return [toGetDayDate(min, 'YYYYMMDD'), toGetDayDate(max, 'YYYYMMDD')]
}
9.获取下月
function getNextMonth() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var min = new Date(year, month + 1, 1);
var max = new Date(year, month + 2, 0);
return [toGetDayDate(min, 'YYYYMMDD'), toGetDayDate(max, 'YYYYMMDD')]
}
10.获取本年
function thisYear() {
var firstDay = new Date();
firstDay.setDate(1);
firstDay.setMonth(0);
var lastDay = new Date();
lastDay.setFullYear(lastDay.getFullYear() + 1);
lastDay.setDate(0);
lastDay.setMonth(-1);
firstDay = toGetDayDate(firstDay, 'YYYYMMDD');
lastDay = toGetDayDate(lastDay, 'YYYYMMDD');
return [firstDay, lastDay];
}
11.获取近30天
//获取近30天
function getRecentDay() {
//获取当前日期
let myDate = new Date();
let nowY = myDate.getFullYear();
let nowM = myDate.getMonth() + 1;
let nowD = myDate.getDate();
let enddate = nowY + "-" + (nowM < 10 ? "0" + nowM : nowM) + "-" + (nowD < 10 ? "0" + nowD: nowD);//当前日期
//获取三十天前日期
let lw = new Date(myDate - 1000 * 60 * 60 * 24 * 30);//最后一个数字30可改,30天的意思
let lastY = lw.getFullYear();
let lastM = lw.getMonth() + 1;
let lastD = lw.getDate();
let startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD);
return [startdate, enddate]
}
12.获取某年某月的最后一天
function mnthLastDay(year, month) {
var nextMonth = month++;
if (nextMonth > 12) {
nextMonth -= 12;
year++;
}
var nextMonthStartDay = new Date(year, nextMonth, 1);
return (new Date(nextMonthStartDay.getTime() - 1000 * 60 * 60 * 24)).getDate();
}
13.比较两个日期大小
function maxSeven(startdate, enddate) {
var starttime = new Date(startdate.replace(/-/g, "\/"));
var endime = new Date(enddate.replace(/-/g, "\/"));
var diff = endime - starttime;
var t = 6 * 24 * 60 * 60 * 1000;
// console.log(t, diff)
//日期大于7天
if (diff !== t) {
return false;
} else {
return true;
}
}