博客
关于我
js中时间戳转时间格式
阅读量:305 次
发布时间:2019-03-04

本文共 1548 字,大约阅读时间需要 5 分钟。

抽空写了一个时间戳转时间格式的方法,供大家参考,有问题留言!

/**时间戳转时间格式         * @params timestamp需要转化的时间戳 format格式类型         * Y-M-D h:m:s 年-月-日(大写) 时:分:秒(小写)         */        function timestampToTimeFormat(timestamp, format = 'Y-M-D h:m') {            var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000            let obj = {                Y: date.getFullYear(),                M: (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),                D: (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()),                h: (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()),                m: (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()),                s: (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())            }            let newitem = format.split("").map(item => {                for (let key in obj) {                    if (item == key) {                        item = obj[key]                    }                }                return item;            })            return newitem.join("");        }        console.log(timestampToTimeFormat(1572432123001)); // 2019-10-30 18:42        console.log(timestampToTimeFormat(1572432123001, 'Y-M-D')); //2019-10-30        console.log(timestampToTimeFormat(1572432123001, 'Y-M-D h:m:s')); //2019-10-30 18:42:03        console.log(timestampToTimeFormat(1572432123001, 'Y/M/D h:m:s')); //2019/10/30 18:42:03        console.log(timestampToTimeFormat(1572432123001, 'Y年M月D日 h:m:s')); //2019年10月30日 18:42:03

转载地址:http://rqoq.baihongyu.com/

你可能感兴趣的文章
2020年制冷与空调设备运行操作答案解析及制冷与空调设备运行操作考试总结
查看>>
2020年保育员(初级)考试资料及保育员(初级)新版试题
查看>>
2020年茶艺师(高级)考试内容及茶艺师(高级)考试申请表
查看>>
2021年重氮化工艺考试题库及重氮化工艺考试报名
查看>>
2021年车工(高级)考试总结及车工(高级)试题及答案
查看>>
2021年压力焊证考试及压力焊实操考试视频
查看>>
2021年低压电工考试及低压电工考试申请表
查看>>
2021年低压电工考试及低压电工考试申请表
查看>>
2021年A特种设备相关管理(电梯)考试APP及A特种设备相关管理(电梯)复审考试
查看>>
2021年N1叉车司机考试题及N1叉车司机复审模拟考试
查看>>
2021年T电梯修理考试技巧及T电梯修理模拟考试软件
查看>>
大数据学习之Spark——00Spark项目的pom.xml文件
查看>>
CodeBlocks开发wxWidgets环境配置详细
查看>>
天涯人脉通讯录 - 设计草图
查看>>
wxWidgets 最新版2.8.11,终于放出来了
查看>>
python学习09:暂停一秒后再输出
查看>>
6、ShardingSphere 之 读写分离
查看>>
C++ STL
查看>>
解方程
查看>>
练习赛 位运算 思维 思维
查看>>