vue事件戳转换日期
时间: 2023-09-07 14:05:22 浏览: 46
Vue事件戳转换日期可以通过使用JavaScript的Date对象来实现。首先,我们可以将事件戳作为参数传递给Date对象的构造函数,然后使用Date对象提供的方法获取所需的日期信息。
以下是一个示例代码,用于将事件戳转换为日期:
```
<template>
<div>
<p>{{ time }}</p>
<p>{{ convertedDate }}</p>
</div>
</template>
<script>
export default {
data() {
return {
time: 1589431431000, // 事件戳
convertedDate: "",
};
},
mounted() {
const dateObj = new Date(this.time);
const year = dateObj.getFullYear();
const month = dateObj.getMonth() + 1;
const day = dateObj.getDate();
const hours = dateObj.getHours();
const minutes = dateObj.getMinutes();
const seconds = dateObj.getSeconds();
this.convertedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
},
};
</script>
```
在上述示例中,我们使用mounted钩子函数来获取日期,并将其格式化为年-月-日 小时:分钟:秒的格式。这样,convertedDate变量将会显示转换后的日期。
通过使用上述代码,您可以将Vue事件戳转换为可读的日期格式。你可以将自己的事件戳替换掉示例代码中的time变量,以获取您所需的日期。