Home  >  Article  >  Web Front-end  >  How to use vue to convert timestamps into custom time formats

How to use vue to convert timestamps into custom time formats

亚连
亚连Original
2018-06-02 09:39:053825browse

下面我就为大家分享一篇vue将时间戳转换成自定义时间格式的方法,具有很好的参考价值,希望对大家有所帮助。

1、首先建立一个date.js文件,写入如下代码:

export 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)) {
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);
};

2、在所要转换的页面引入date.js文件:

import {formatDate} from '../../date.js';

3、调用方法如下:

formatDate(new Date(time * 1000), 'yyyy-MM-dd hh:mm');

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

相关文章:

详细介绍JsChart组件使用方法以及功能

通过在ionic2中使用自动生成器的方法步骤有哪些?

通过AjaxUpLoad.js实现文件上传的代码示例(详细教程)

The above is the detailed content of How to use vue to convert timestamps into custom time formats. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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