赞
踩
一个比较简单的功能吧,但是容易忘记,所以记录一下,忘记的时候可以翻翻,大家忘记的时候也可以看看。
这个功能可以通过JavaScript的Date对象来实现。
- <template>
- <div>
- 当前日期:{{ currentDate }} <!-- 显示实时日期 -->
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- currentDate: '',
- };
- },
- mounted() {
- this.getCurrentDate();
- setInterval(() => {
- this.getCurrentDate();
- }, 1000); // 每秒钟更新一次日期
- },
- methods: {
- getCurrentDate() {
- const date = new Date();
- const year = date.getFullYear(); // 获取年份
- const month = date.getMonth() + 1; // 获取月份(注意月份从0开始,所以要加1)
- const day = date.getDate(); // 获取日期
-
- this.currentDate = year + '年' + month + '月' + day + '日';
- },
- },
- };
- </script>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
总结:通过Date对象获取实时年、月、日,然后赋值给year、month、day,最后赋值给currentDate,输出即可。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。