posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
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;
}
复制代码

 

posted on   邢帅杰  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2016-11-21 log4net的使用
点击右上角即可分享
微信分享提示