获取当前年月日 时分秒
时间: 2023-06-28 19:15:54 浏览: 67
你可以使用JavaScript内置的`Date`对象来获取当前的年月日时分秒。以下是一个示例:
```
<template>
<div>
<p>{{ currentDate }}</p>
</div>
</template>
<script>
export default {
data() {
return {
currentDate: "",
};
},
created() {
setInterval(() => {
const date = new Date();
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);
this.currentDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}, 1000);
},
};
</script>
```
在这个示例中,我们定义了一个数据属性`currentDate`来存储当前的年月日时分秒。在`created()`方法中,我们使用`setInterval()`方法每秒钟更新一次`currentDate`。我们首先创建一个`Date`对象来获取当前的日期和时间,然后使用`getFullYear()`、`getMonth()`、`getDate()`、`getHours()`、`getMinutes()`和`getSeconds()`方法来获取年、月、日、时、分和秒。最后,我们将这些值组合成一个字符串,并将其赋值给`currentDate`属性,以便在模板中使用插值表达式`{{ currentDate }}`将其显示在页面上。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)