Home > Web Front-end > JS Tutorial > How to convert timestamp format in js

How to convert timestamp format in js

亚连
Release: 2018-06-14 17:45:13
Original
1999 people have browsed it

下面我就为大家分享一篇js实现把时间戳转换为yyyy-MM-dd hh:mm 格式(es6语法),具有很的参考价值,希望对大家有所帮助

如下所示:

function formatDate(date,fmt) {
 if(/(y+)/.test(fmt)){
 fmt = fmt.replace(RegExp.$1,(date.getFullYear()+'').substr(4-RegExp.$1.length));
 }
 let o = {
 'M+':date.getMonth() + 1,
 'd+':date.getDate(),
 'h+':date.getHours(),
 'm+':date.getMinutes(),
 's+':date.getSeconds()
 };

 // 遍历这个对象
 for(let k in o){
 if(new RegExp(`(${k})`).test(fmt)){
  // console.log(`${k}`)
  console.log(RegExp.$1)
  let str = o[k] + '';
  fmt = fmt.replace(RegExp.$1,(RegExp.$1.length===1)?str:padLeftZero(str));
 }
 }
 return fmt;
};

function padLeftZero(str) {
 return ('00'+str).substr(str.length);
}
Copy after login

let TimeNow = 1514374627*1000
let newTime = new Date(TimeNow)

formatDate(newTime,'yyyy-MM-dd hh:mm')   //2017-12-27 19:37

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

使用vue2.0.js实现多级联动选择器

通过JavaScript实现比较同一天的时间大小

在React、Vue项目中如何使用SVG

使用react实现分页组件

VUE2实现二级省市联动选择

在mint-ui中使用时间插件及获取选择值

在vue中全选实现数据的绑定及获取

详解讲解使用jest测试react native组件

The above is the detailed content of How to convert timestamp format in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template