code
//日期戳转日期字符串:yyyy-MM-dd HH:mm:ss export const formatDate = (v: string | number | Date) => { if (v == null) { return ''; } else { const dateObj = new Date(v); // 创建Date对象 const year = dateObj.getFullYear(); // 获取年份 const month = ("0" + (dateObj.getMonth() + 1)).slice(-2); // 获取月份,并补零 const day = ("0" + dateObj.getDate()).slice(-2); // 获取日期,并补零 const hour = ("0" + dateObj.getHours()).slice(-2); // 获取小时,并补零 const minute = ("0" + dateObj.getMinutes()).slice(-2); // 获取分钟,并补零 const second = ("0" + dateObj.getSeconds()).slice(-2); // 获取秒,并补零 return `${year}-${month}-${day} ${hour}:${minute}:${second}`; // 返回转换后的日期格式 } } //日期戳转日期字符串:yyyy-MM-dd export const formatDateShort = (v: string | number | Date) => { let fullDateStr = formatDate(v); if (fullDateStr != '') { fullDateStr = fullDateStr.substring(0, 11); } return fullDateStr; } // 格式化日期 yyyy年-MM月-dd日 export const formatDateShortCN = (val: string | number | Date) => { const dateObj = new Date(val) const year = dateObj.getFullYear(); // 获取年份 const month = ("0" + (dateObj.getMonth() + 1)).slice(-2); // 获取月份,并补零 const day = ("0" + dateObj.getDate()).slice(-2); // 获取日期,并补零 return year + '年-' + month + '月-' + day + '日'; } //获取时间戳 export function getNowTimeSpan() { return new Date().getTime();//.toString() } //获取时间戳 export function getDateTimeSpan(date: Date) { return date.getTime();//.toString() } //修改日期,增加或减少(负数)指定日期段,判断闰2月 export const dateAdd = (date: string | number | Date, strInterval: string, num: number) => { //日期 var dt = new Date(date); //传入日期转日期戳 var dtstp = dt.getTime(); switch (strInterval) { case 'sec': return new Date(dtstp + (1000 * num));//秒 case 'min': return new Date(dtstp + (60000 * num));//分 case 'hour': return new Date(dtstp + (3600000 * num));//小时 case 'day': return new Date(dtstp + (86400000 * num));//天 case 'weekend': return new Date(dtstp + ((86400000 * 7) * num));//周 //季度(三个月) case 'quarter': let monthQ = (dt.getMonth()) + (num * 3); let dQ = dt.getDate(); //如果是2月,判断28天还是29天。 let monthCal = (monthQ + 1) % 12; if (monthCal == 0) { //等于0,说明是12月。 monthCal = 12; } if (monthCal == 2 && dQ > 28) { let testDate = new Date(dt.getFullYear(), monthQ, 29); if (testDate.getDate() == 29) { dQ = 29; } else { dQ = 28; } } return new Date(dt.getFullYear(), monthQ, dQ, dt.getHours(), dt.getMinutes(), dt.getSeconds()); //月 case 'month': let month = (dt.getMonth()) + num; let d = dt.getDate(); //如果是2月,判断28天还是29天。 let monthCal2 = (month + 1) % 12; if (monthCal2 == 0) { //等于0,说明是12月。 monthCal2 = 12; } if (monthCal2 == 2 && d > 28) { let testDate2 = new Date(dt.getFullYear(), month, 29); if (testDate2.getDate() == 29) { d = 29; } else { d = 28; } } return new Date(dt.getFullYear(), month, d, dt.getHours(), dt.getMinutes(), dt.getSeconds()); //年 case 'year': return new Date((dt.getFullYear() + num), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds()); } return dt; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2016-11-21 log4net的使用