Commit 0ebacfe8 by caoyanzhi

update: 日期格式化方法

parent c007357a
......@@ -8,8 +8,10 @@
<script>
import { projectName } from '@/config/index.js';
import formatters from '../utils/formatters.js';
export default {
name: 'App',
provide: { formatters },
data() {
return {
projectName,
......
// 在vue文件中使用inject: ['formatters'],即可通过formatters的属性访问格式化方法
export default {
/**
* @desc 格式化日期
* @param {*} date Date对象或者时间戳
* @param {*} type 日期格式,支持yyyy-MM-dd HH:mm:ss的任意组合,默认返回yyyy-MM-dd HH:mm:ss
* @returns
*/
formatDate(date, type = 'yyyy-MM-dd HH:mm:ss') {
if (!date) return '--';
const d = new Date(date);
const result = {
yyyy: d.getFullYear(),
MM: (d.getMonth() + 1).toString().padStart(2, '0'),
dd: d.getDate().toString().padStart(2, '0'),
HH: d.getHours().toString().padStart(2, '0'),
mm: d.getMinutes().toString().padStart(2, '0'),
ss: d.getSeconds().toString().padStart(2, '0'),
};
Object.keys(result).forEach((k) => {
type = type.replace(k, result[k]);
});
return type;
},
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment