vue的时间格式转换
时间: 2024-01-13 19:20:10 浏览: 24
在Vue中进行时间格式转换可以使用过滤器或者计算属性来实现。下面是两种方法的示例:
1. 使用过滤器:
```html
<template>
<div>
<p>原始时间格式:{{ time }}</p>
<p>转换后的时间格式:{{ time | dateFormat }}</p>
</div>
</template>
<script>
export default {
data() {
return {
time: '2022-05-27T14:20:27.000000Z'
};
},
filters: {
dateFormat(value) {
const date = new Date(value);
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
}
};
</script>
```
2. 使用计算属性:
```html
<template>
<div>
<p>原始时间格式:{{ time }}</p>
<p>转换后的时间格式:{{ formattedTime }}</p>
</div>
</template>
<script>
export default {
data() {
return {
time: '2022-05-27T14:20:27.000000Z'
};
},
computed: {
formattedTime() {
const date = new Date(this.time);
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
}
};
</script>
```