1、日期格式化
/**
* @description: 日期格式化函数1
* @param date 日期类型
* @param format 日期格式 默认 yyyy-MM-dd HH:mm:ss格式
*/
function transform(date, format = 'yyyy-MM-dd HH:mm:ss') {
const theCurrentDate = new Date(date.getTime()); // 转换日期格式
const year = theCurrentDate.getFullYear().toString();
format = format.replace('yyyy', year); // 替换年份
const month = (theCurrentDate.getMonth() + 1).toString().padStart(2, '0');
format = format.replace('MM', month); // 替换月份
const day = theCurrentDate.getDate().toString().padStart(2, '0');
format = format.replace('dd', day); // 替换天
const hour = theCurrentDate.getHours().toString().padStart(2, '0');
format = format.replace('HH', hour); // 替换小时
const minutes = theCurrentDate.getMinutes().toString().padStart(2, '0');
format = format.replace('mm', minutes); // 替换分钟
const second = theCurrentDate.getSeconds().toString().padStart(2, '0');
format = format.replace('ss', second); // 替换秒
return format;
}
/**
* @description: 日期格式化函数2
* @param date 日期类型
* @param format 日期格式 默认 yyyy-MM-dd HH:mm:ss格式
*/
function formatDate(date, format = 'yyyy-MM-dd HH:mm:ss 星期w') {
if (!date) {
return date;
}
const typeDate = date instanceof Date ? date.getTime() : date;
date = new Date(typeDate);
const obj = {
yyyy: date.getFullYear(), // 完整年份 例:2021 -> 2021
yy: ('' + date.getFullYear()).slice(-2), // 缩写年份 例:2021 -> 21
M: date.getMonth() + 1, // 月份 不足两位不补0
MM: ('0' + (date.getMonth() + 1)).slice(-2), // 月份 不足两位补0
d: date.getDate(), // 天 不足两位不补0
dd: ('0' + date.getDate()).slice(-2), // 天 不足两位补0
H: date.getHours(), // 24小时 不足两位不补0
HH: ('0' + date.getHours()).slice(-2), // 24小时 不足两位补0
h: date.getHours() % 12, // 12小时制 不足两位不补0
hh: ('0' + (date.getHours() % 12)).slice(-2), // 12小时制 不足两位补0
m: date.getMinutes(), // 分钟 不足两位不补0
mm: ('0' + date.getMinutes()).slice(-2), // 分钟 不足两位补0
s: date.getSeconds(), // 秒 不足两位不补0
ss: ('0' + date.getSeconds()).slice(-2), // 秒 不足两位补0
w: ['日', '一', '二', '三', '四', '五', '六'][date.getDay()], // 星期
};
return format.replace(/([a-z]+)/gi, function (key) {
return obj[key];
});
}
console.log(formatDate('2021-07-03 19:00:00'));
// 2021-07-03 19:00:00 星期六
2、倒推时间
/**
* @description: 往前倒推小时
* @param date 日期
* @param num 需要倒推的小时 如果为负数则为往后倒推小时
* @return 返回日期格式
*/
function beforeHours(date, num) {
if (!date) {
return date;
}
const typeDate = date instanceof Date ? date.getTime() : date;
date = new Date(typeDate);
const theCurrentDate = new Date(date.getTime());
theCurrentDate.setHours(theCurrentDate.getHours() - (num || 0));
return theCurrentDate;
}
/**
* @description: 往前倒推天数
* @param date 日期
* @param num 需要倒推的天数 如果为负数则为往后倒推天数
* @return 返回日期格式
*/
function beforeDays(date, num) {
if (!date) {
return date;
}
const typeDate = date instanceof Date ? date.getTime() : date;
date = new Date(typeDate);
const theCurrentDate = new Date(date.getTime());
theCurrentDate.setDate(theCurrentDate.getDate() - (num || 0));
return theCurrentDate;
}
/**
* @description: 往前倒推月份
* @param date 日期
* @param num 需要倒推的月份 如果为负数则为往后倒推月份
* @return 返回日期格式
*/
function beforeMonths(date, num) {
if (!date) {
return date;
}
const typeDate = date instanceof Date ? date.getTime() : date;
date = new Date(typeDate);
const theCurrentDate = new Date(date.getTime());
theCurrentDate.setMonth(theCurrentDate.getMonth() - (num || 0));
return theCurrentDate;
}
测试:
console.log(formatDate(beforeMonths('2021-07-03 12:00:00', 2)));
console.log(formatDate(beforeDays('2021-07-03 12:00:00', 4)));
console.log(formatDate(beforeHours('2021-07-03 12:00:00', 5)));
console.log(formatDate(beforeHours('2021-07-03 12:00:00', -1)));
3、根据年份月份计算当月天数
/**
* @description: 根据年份月份计算当月天数
* @param year 年份
* @param month 月份
* @return 返回日期格式
*/
function calculateDaysByYM(year, month) {
// 该函数没有对参数进行校验 必须确保传入年份月份为正确的数字
year = parseInt(year, 10);
month = parseInt(month, 10);
let days;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
// 判断是否闰年
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
days = 29;
} else {
days = 28;
}
}
return days;
}
4、计算两个日期相差的天数
/**
* @description: 计算两个日期相差的天数
* @param sDate1 日期1
* @param sDate2 日期2
* @return 返回天数
*/
function datedifference(sDate1, sDate2) {
const dateSpan = Math.abs(Date.parse(sDate2) - Date.parse(sDate1));
return Math.floor(dateSpan / (24 * 3600 * 1000));
}
不定期更新收集 喜欢的点点赞哦~