博客
关于我
js中时间戳转时间格式
阅读量:306 次
发布时间: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/

你可能感兴趣的文章
【MapReduce】基础案例 ---- 自定义OutputFormat <根据内容输出到指定文件目录中>
查看>>
【Android】 模拟器上运行程序报错
查看>>
【sklearn练习】KMeans ---- iris(鸢尾花)数据集聚类评估
查看>>
【HTML5 CSS】display和visibility的区别
查看>>
java线程(4)——使用多个线程操作同一个对象(买票的例子)
查看>>
前端HTML中表单action属性的作用
查看>>
java线程(17)——Lock锁,三个线程抢票加上lock锁后变成三个线程排队买票
查看>>
java线程(19)——信号灯法,电视播放,生产者与消费者的案例
查看>>
java线程(20)——Runnable线程池
查看>>
计算机网络ip知识点
查看>>
react(3)——导入了正确的包,但是运行不出来,原因是因为导入包的顺序有问题
查看>>
react(10)——三大属性state,props,refs,总结其特点
查看>>
javascript(7)——定义函数的两种方式以及使用的时候传入多个参数的情况,arguments和...rest
查看>>
react(20)——使用函数的柯里化实现获取表单的数据
查看>>
react(28)——react脚手架public目录下文件讲解
查看>>
Visual Studio Code——安装react插件,提高代码速率
查看>>
mybatis(3)——使用map参数进行添加用户案列
查看>>
看领导者系列第二部《成为领导者》的几点感悟
查看>>
mybatis(11)——在mybatis中配置并使用log4j日志
查看>>
mybatis(12)——在mybatis中通过limit和RowBounds实现分页查询
查看>>