js中国标准时间转换

一、将中国标准时间转换为 例如 2023-12-18 08:00:00

 // 获取今天的日期
                let today = new Date();
                // 设置 beginDate 为今天的上午8点
                let beginDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 8, 0, 0, 0);
                // 设置 finishDate 为 beginDate 的后三天的0点
                let finishDate = new Date(beginDate.getFullYear(), beginDate.getMonth(), beginDate.getDate() + 3, 0, 0, 0,0);
                // 设置 publicityDate 为 finishDate 的后三天的0点
                let publicityDate = new Date(finishDate.getFullYear(), finishDate.getMonth(), finishDate.getDate() + 3, 0, 0, 0,0);
                beginDate=formatDate(beginDate);
                finishDate=formatDate(finishDate);
                publicityDate=formatDate(publicityDate);

主要方法:

    function formatDate(date){
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h =date.getHours()
        h = h < 10 ? ('0' + h) : h
        let M =date.getMinutes()
        M = M < 10 ? ('0' + M) : M
        let s =date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;
        return dateTime;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值