/*
* 时间转字符串
* dateToStr(new Date(),'YYYY年MM月DD天');
* dateToStr(new Date(),'YYYY-MM-DD');
* dateToStr(new Date(),false);
*/
const dateToStr = (d, format = false) => {
const date = new Date(d);
const config = {
YYYY: date.getFullYear(),
MM: addZero(date.getMonth() + 1),
DD: addZero(date.getDate()),
HH: addZero(date.getHours()),
mm: addZero(date.getMinutes()),
ss: addZero(date.getSeconds()),
}
//布尔类型
if (typeof format === 'boolean') {
if (format) {
return dateToStr(d, 'YYYY-MM-DD HH:mm:ss');
} else {
return dateToStr(d, 'YYYY-MM-DD');
}
}
for (const key in config) {
format = format.replace(key, config[key])
}
return format;
}
//日期补零操作
function addZero(num) {
if (parseInt(num) < 10) {
num = '0' + num;
}
return num;
}
/***********************************************/
//判断日期是否同一天
const dayIsSame = (time1, time2) => {
const timeMs1 = new Date(time1).setHours(0, 0, 0, 0);
const timeMs2 = new Date(time2).setHours(0, 0, 0, 0);
return timeMs1 === timeMs2 ? true : false
}
/***********************************************/
//比较两个日期的差数
const dateDiff=(time1, time2,type)=>{
let diff = Math.abs(new Date(time1) - new Date(time2));
//相差秒数
if (type==='second') return diff/1000;
//相差分钟
if (type==='minute') return diff/1000/60;
//相差小时
if (type==='hour') return diff/1000/60/60;
//相差天
if (type==='day') return diff/1000/60/60/24;
return {
days: Math.floor(diff / (1000 * 60 * 60 * 24)),
hour: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
seconds: Math.floor((diff % (1000 * 60)) / 1000),
}
}
/***********************************************/
/**
合同日期生成
console.log( dayLater('2023-01-29', 1, 'month') );//2023-02-28
console.log( dayLater('2024-01-31',1,'month') ); //2024-02-28
console.log( dayLater('2023-01-31',1,'month') );//2023-02-28
console.log( dayLater('2024-2-29',1,'month') );//2024-03-29
console.log( dayLater('2023-03-31',1,'month') );//2023-04-30
console.log( dayLater('2024-12-31',-1,'month') );//2023-11-30
console.log( dayLater('2024-02-29',-1,'month') );//2023-01-29
console.log( dayLater('2024-03-31',-1,'month') );//2023-02-28
console.log( dayLater('2024-2-29',1,'year') );//2025-02-28
console.log( dayLater('2000-01-31',1,'month') ); //2024-02-28
*/
const dayLater = (startTime=new Date(), stepNumber, stepType = 'day',format=false) => {
let date = new Date(startTime);
let curDay=date.getDate();
let curMonth=date.getMonth()+1;
let curYear=date.getFullYear();
switch (stepType) {
case 'second':
date.setSeconds(date.getSeconds() + stepNumber);
break;
case 'hour':
date.setHours(date.getHours() + stepNumber);
break;
case 'day':
date.setDate(date.getDate() + stepNumber);
break;
case 'month':
//这里无法考虑 1月31号,1月之后,应当是2月28
// date.setMonth(date.getMonth() + stepNumber);
//考虑 1月31号,1月之后。改为2月最后一天
let stepYear=Math.floor(Number(stepNumber)/12);
let targetMonth = (curMonth + stepNumber)%12;
//目标日期,所在月共多少天
let targetDayCount=new Date(curYear+stepYear,targetMonth,0).getDate();
curDay = curDay>targetDayCount?targetDayCount:curDay;
if (curMonth+Number(stepNumber) >12 ){
date = new Date(`${curYear+stepYear+1}-${targetMonth}-${curDay}`);
}else {
date = new Date(`${curYear+stepYear}-${targetMonth}-${curDay}`);
}
break;
case 'year':
//这里无法考虑 2月29号之后,N年后可能是2月28号
// date.setMonth(date.getFullYear() + stepNumber);
if ((curYear%4===0||curYear%100===0||curYear%400===0) && curMonth===2 && curDay===29){
date = new Date(`${curYear+Number(stepNumber)}-02-28`);
}else{
date.setFullYear(date.getFullYear() + Number(stepNumber));
}
break;
}
return dateToStr(date,format)
}
js 时间常用函数封装 JavaScript封装时间函数
于 2022-08-23 16:58:47 首次发布
这篇博客介绍了JavaScript中将日期转换为字符串的函数`dateToStr`,支持多种格式,并提供了一个辅助函数`addZero`用于补零。此外,还导出了一个`isSameDay`函数,用于判断两个时间是否属于同一天,主要通过比较日期的毫秒值实现。
摘要由CSDN通过智能技术生成