const localDateTimeFormat = 'YYYY-MM-DD HH:mm:ss';
const toStrDateFormat = 'YYYY-MM-DD';
const localTimeFormat = 'HH:mm:ss';
//返回世界协调时间
function utcNow(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.toUTCString();
}
//根据本地时间格式,将date对象转换成字符串
function toLocalString(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.toLocaleString();
}
//根据本地时间格式,将Date对象的时间部分转换为字符串
function toLocalTimeString(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.toLocaleTimeString();
}
//根据本地时间格式,将date对象的日期部分转换成字符串
function toLocalDateString(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.toLocaleDateString();
}
//返回date对象的原始值
function dateValueOf(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.valueOf();
}
//返回当前日期时间(字符串)
function getDateTimeStr(fmt) {
const date = new Date()
return dateFormat(date, fmt).toString()
}
//返回当前日期时间(Date)
function getToDayDateTime() {
const date = new Date()
return new Date(dateFormat(date))
}
function getDateStr(fmt) {
fmt = fmt || toStrDateFormat;
const date = new Date()
return dateFormat(date, fmt).toString()
}
function getTimeStr(fmt) {
fmt = fmt || localTimeFormat;
const date = new Date()
return dateFormat(date, fmt).toString()
}
//返回当前日期(中国标准时间)
function getDate() {
return getToDayDateTime();
}
//返回当前年份
function year(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getFullYear();
}
//当前月份
function month(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getMonth();
}
//返回表示指定 date 的“日”部分的整数
function day(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getDay();
}
//当前小时
function hour(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getHours();
}
// 当前毫秒
function milliSecond(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getMilliseconds();
}
// 当前分钟
function minute(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getMinutes();
}
//当前秒
function second(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getSeconds();
}
//当前星期几(0为星期一)
function dayOfWeek(date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return curdate.getDay(); //返回星期几(0是周日)
}
//日期类加减运算
/**
* 年
* @param {*} date
* @param {*} number
*/
function addQuarters(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setMonth(curdate.getMonth() + number * 3));
}
//增加月
function addMonths(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setMonth(curdate.getMonth() + number));
}
//增加周
function addWeeks(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setDate(curdate.getDate() + number * 7));
}
//增加天数
function addDays(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setDate(curdate.getDate + number));
}
//增加小时数
function addHours(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setHours(curdate.getHours() + number));
}
//增加分钟数
function addMinutes(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setMinutes(curdate.getMinutes() + number));
}
//增加秒数
function addSeconds(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setSeconds(curdate.getSeconds() + number));
}
//增加毫秒数
function addMilliSeconds(number, date) {
const curdate = date ? new Date(date) : getToDayDateTime(); //若不输入date参数,则用当前日期替代
return new Date(curdate.setUTCMilliseconds(curdate.getMilliseconds() + number));
}
//日期类-两个日期运算处理 年 月 日 天 小时 分钟 秒 毫秒
//工龄/年龄计算
function age(dateOne) {
if (isNotBlank(dateOne)) {
dateOne = new Date(dateOne);
} else dateOne = new Date();
const dateTwo = new Date();
let diff = Math.abs(dateTwo.getFullYear() - dateOne.getFullYear());
return diff;
}
//年份差
function diffYear(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let diff = Math.abs(dateTwo.getFullYear() - dateOne.getFullYear());
return diff;
}
//月份差
function diffMonth(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let diff = Math.abs(dateTwo.getMonth() - dateOne.getMonth());
return diff;
}
//天数差
function diffDay(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let dateSpan, diff;
dateSpan = Math.abs(dateTwo - dateOne);
diff = Math.floor(dateSpan / (24 * 3600 * 1000));
return diff;
}
//小时差
function diffHour(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let diff = Math.floor(Math.abs((dateOne.getTime() - dateTwo.getTime()) / 1000 / 3600));
return diff;
}
// 分钟差
function diffMinute(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let diff = Math.floor(Math.abs((dateOne.getTime() - dateTwo.getTime()) / 1000 / 60));
return diff;
}
//秒差
function diffSecond(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let diff = Math.floor(Math.abs((dateOne.getTime() - dateTwo.getTime()) / 1000));
return diff;
}
//毫秒差
function diffMilliSecond(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
let diff = Math.floor(Math.abs(dateOne.getTime() - dateTwo.getTime()));
return diff;
}
/**
* 计算两个日期的时间差(精确到天、小时、分、秒)
* @param {*} dateOne
* @param {*} dateTwo
*/
function diffDate(dateOne, dateTwo) {
dateOne = dateOne ? new Date(dateOne) : getToDayDateTime();
dateTwo = dateTwo ? new Date(dateTwo) : getToDayDateTime();
var dateDiff = Math.abs(dateTwo.getTime() - dateOne.getTime()); //时间差的毫秒数
var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //相差天数
var ms1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(ms1 / (3600 * 1000)); //计算出小时
var ms2 = ms1 % (3600 * 1000); //计算小时后剩余的毫秒数
var minutes = Math.floor(ms2 / (60 * 1000)); //计算相差的分钟数
var ms3 = ms2 % (60 * 1000); //计算剩余的毫秒数
var seconds = Math.round(ms3 / 1000); //计算秒
let result = {
days: dayDiff,
hours: hours,
minutes: minutes,
seconds: seconds,
};
return result;
}
/**
* 格式化显示日期
* @param {*} value 日期
* @param {*} exp 时间日期显示格式
*/
function dateFormat(value, fmt) {
value = new Date(value);
fmt = fmt || localDateTimeFormat; //默认格式
var d = {
'M+': value.getMonth() + 1,
'D+': value.getDate(),
'H+': value.getHours(),
'm+': value.getMinutes(),
's+': value.getSeconds(),
'q+': Math.floor((value.getMonth() + 3) / 3),
S: value.getMilliseconds(),
};
if (/(Y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (value.getFullYear() + '').substr(4 - RegExp.$1.length));
for (var k in d)
if (new RegExp('(' + k + ')').test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? d[k] : ('00' + d[k]).substr(('' + d[k]).length)
);
return fmt;
}
//日期类-[通用比较逻辑]
//晚于今年
function isLaterThisYear(date) {
let current = new Date();
date = new Date(date);
if (date.getFullYear() - current.getFullYear() > 0) {
return true;
}
return false;
}
//早于今年
function isEarlierThisYear(date) {
let current = new Date();
date = new Date(date);
if (date.getFullYear() - current.getFullYear < 0) {
return true;
}
return false;
}
//去年
function isLastYear(date) {
let current = new Date();
date = new Date(date);
if (current.getFullYear() - date.getFullYear() == 1) {
return true;
}
return false;
}
//明年
function isNextYear(date) {
let current = new Date();
date = new Date(date);
if (date.getFullYear() - current.getFullYear() == 1) {
return true;
}
return false;
}
//今年
function isThisYear(date) {
let current = new Date();
date = new Date(date);
if (date.getFullYear() - current.getFullYear() == 0) {
return true;
}
return false;
}
//晚于本月
function isLaterThisMonth(date) {
let current = new Date();
date = new Date(date);
if (
date.getFullYear() * 12 +
(date.getMonth() + 1) -
(current.getFullYear() * 12 + (current.getMonth() + 1)) >
1
) {
return true;
}
return false;
}
//早于本月
function isEarlierThisMonth(date) {
let current = new Date();
date = new Date(date);
if (
current.getFullYear() * 12 +
(current.getMonth() + 1) -
(date.getFullYear() * 12 + (date.getMonth() + 1)) >
1
) {
return true;
}
return false;
}
//本月
function isThisMonth(date) {
let current = new Date();
date = new Date(date);
if (
date.getFullYear() * 12 +
(date.getMonth() + 1) -
(current.getFullYear() * 12 + (current.getMonth() + 1)) ==
0
) {
return true;
}
return false;
}
//下个月
function isNextMonth(date) {
let current = new Date();
date = new Date(date);
if (
date.getFullYear() * 12 +
(date.getMonth() + 1) -
(current.getFullYear() * 12 + (current.getMonth() + 1)) ==
1
) {
return true;
}
return false;
}
//获取前一周的时间(Date)
function getLastWeek(date) {
date = date ? new Date(date) : getToDayDateTime()
var week = new Date(date - 7 * 24 * 3600 * 1000)
return dateFormat(week)
}
//早于本周
function isEarlierThisWeek(date) {
let current = new Date();
date = new Date(date);
let startWeek = 0 - current.getDay();
if (startWeek > diffDay(date, current)) {
return true;
}
return false;
}
//晚于本周
function isLaterThisWeek(date) {
let current = new Date();
date = new Date(date);
let endWeek = 6 - current.getDay();
if (diffDay(date, current) > endWeek) {
return true;
}
return false;
}
//是上一周 TODO
function isLastWeek(date) {
let current = new Date();
date = new Date(date);
let endLastWeek = current.getDay() + 7;
let startLastWeek = current.getDay();
let result = diffDay(date, current);
if (startLastWeek < result && result < endLastWeek) {
return true;
}
return false;
}
//是下一周
function isNextWeek(date) {
let current = new Date();
date = new Date(date);
let startLastWeek = 6 - current.getDay();// 0-6 [周日-周六]
let endLastWeek = 7 + current.getDay();
let result = diffDay(date, current);
if (startLastWeek < result && result < endLastWeek) {
return true;
}
return false;
}
//两周之后
function isTwoWeeksLater(date) {
let current = new Date();
date = new Date(date);
if (diffDay(date, current) > 6 - current.getDay() + 7) {
return true;
}
return false;
}
//本周
function isThisWeek(date) {
let current = new Date();
date = new Date(date);
let startWeek = 0 - current.getDay();
let endWeek = 6 - current.getDay();
let result = diffDay(date, current);
if (startWeek < result && result < endWeek) {
return true;
}
return false;
}
//今天
function isToday(date) {
let current = new Date();
date = new Date(date);
if (Math.floor(Math.abs(date.getTime() - current.getTime()) / (24 * 3600 * 1000)) == 0) {
return true;
}
return false;
}
//明天
function isTomorrow(date) {
let current = new Date();
date = new Date(date);
const result = Math.floor(Math.abs(date.getTime() - current.getTime()) / (24 * 3600 * 1000))
if (0 < result && result < 1) {
return true;
}
return false;
}
//昨天
function isYesterday(date) {
let current = new Date();
date = new Date(date);
if (Math.floor(Math.abs(current.getTime() - date.getTime()) / (24 * 3600 * 1000)) == 1) {
return true;
}
return false;
}
//明天之后
function isDayAfterTomorrow(date) {
let current = new Date();
date = new Date(date);
if (Math.abs(date.getTime() - current.getTime()) / (24 * 3600 * 1000) > 1) {
return true;
}
return false;
}