js快速获取当前时间并且返回想要的格式

function backCurrentTime (type) {
    let currentTime=new Date( new Date() + 8 * 3600 * 1000 ).toJSON().substr(0,19).replace("T"," ");
    if(type=='y'){
        return currentTime.substr(0,4)
    }else if(type=='m'){
        return currentTime.substr(5,2)
    }else if(type=='d'){
        return currentTime.substr(8,2)
    }else if(type=='y-m'){
        return currentTime.substr(0,7)
    }else if(type=='y-m-d'){
        return currentTime.substr(0,10)
    }else if(type=='y-m-d-h-m'){
        return currentTime.substr(0,16)
    }else{
        return currentTime
    }
}
console.log('当前年',backCurrentTime('y') )
console.log('当前月',backCurrentTime('m') )
console.log('当前天',backCurrentTime('d') )
console.log('当前年-月-天',backCurrentTime('y-m-d') )
console.log('当前年-月-天-时-分',backCurrentTime('y-m-d-h-m') )
主要是利用toJSON转格林威治时间 在+8小时得到就是北京时间
参考地址:https://www.cnblogs.com/youwei716/p/14088544.html

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...