当前位置: 首页 > news >正文

vue实现获取当前时间并实时显示

以下代码可以实现获取当前时间并实时显示,朋友们直接copy使用即可,希望可以帮助到有需要的朋友们!

<template><div class="time">{{ datetimeStr }}</div>
</template>
<script>export default {data() {return {datetimeStr: '', // 显示当前时间formatDate: null, // 定时器}}}mounted() {this.updateDateTime();this.formatDate = setInterval(() => {this.updateDateTime();}, 1000);}unmounted() { // 在Vue实例销毁前,清除时间定时器if (this.formatDate) {clearInterval(this.formatDate);}}methods: {updateDateTime() { // 格式化需要展示的时间格式const now = new Date();const year = now.getFullYear();let month = now.getMonth() + 1;let day = now.getDate();const weekday = now.getDay();let hours = now.getHours();let minutes = now.getMinutes();let seconds = now.getSeconds();// 将月份和日期补零month = month < 10 ? '0' + month : month;day = day < 10 ? '0' + day : day;hours = hours < 10 ? '0' + hours : hours;minutes = minutes < 10 ? '0' + minutes : minutes;seconds = seconds < 10 ? '0' + seconds : seconds;// 星期数组const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];// 格式化日期时间this.datetimeStr = year + "年" + month + "月" + day + "日 " + weekdays[weekday] + " " + hours + ":" + minutes + ":" + seconds;}}}
</script>
http://www.lryc.cn/news/460143.html

相关文章:

  • 【论文阅读】SRCNN
  • 数据结构与算法——Java实现 32.堆
  • 深度学习 .dot()
  • idea2024 git merge 时丢失 Merge remote-tracking branch问题
  • pdf怎么删除多余不想要的页面?删除pdf多余页面的多个方法
  • 树莓派应用--AI项目实战篇来啦-3.OpenCV 读取写入和显示图像
  • 一句话就把HTTPS工作原理讲明白了
  • CPU 和处理核心(Core)中间有3个缓存
  • 前后分离项目记录
  • 一句话木马的多种变形方式
  • 【NestJS入门到精通】装饰器
  • XML 编辑最简单好用的 QXmlEdit 软件已经完整中文化
  • ref标签、style的scope
  • 22年408数据结构
  • ubuntu 虚拟机将linux文件夹映射为windows网络位置
  • Pytho逻辑回归算法:面向对象的实现与案例详解
  • AWS WAF实战指南:从入门到精通
  • k8s的部署
  • C# 两个进程/exe通讯方式 两个应用程序通讯方式
  • ubuntu下打开摄像头
  • ABAP 表转JSON格式
  • oceanbase的日志量太大,撑爆磁盘,修改下日志级别
  • 【C++11】lambda表达式
  • 前端学习-css的背景(十六)
  • 使用Postman搞定各种接口token实战
  • ssh连接慢的问题或远程连接服务超时
  • 基于卷积神经网络的蔬菜识别系统,resnet50,mobilenet模型【pytorch框架+python源码】
  • 数据结构与算法:栈与队列的高级应用
  • macos php开发环境之macport安装的php扩展安装,php常用扩展安装,port中可用的所有php扩展列表
  • 使用Pytorch+Numpy+Matplotlib实现手写字体分类和图像显示