vue中如何获取当时时间时分秒

在 Vue 中可以使用 JavaScript 中的 Date 对象来获取当前时间,然后使用 Vue 中的数据绑定将时间显示在页面上。

html 复制代码
<template>
  <div>
    <p>当前时间:{{ time }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      time: ''
    }
  },
  mounted() {
    this.getTime()
  },
  methods: {
    getTime() {
      const date = new Date()
      const hours = date.getHours()
      const minutes = date.getMinutes()
      const seconds = date.getSeconds()
      this.time = `${hours}:${minutes}:${seconds}`
      setInterval(() => {
        const date = new Date()
        const hours = date.getHours()
        const minutes = date.getMinutes()
        const seconds = date.getSeconds()
        this.time = `${hours}:${minutes}:${seconds}`
      }, 1000)
    }
  }
}
</script>

在上面的示例中,mounted 钩子函数会在组件挂载后立即执行 this.getTime() 方法来获取时间。getTime() 方法使用 setInterval 来定时获取时间,并将时间格式化成时分秒的形式,然后将其赋值给 this.time 变量。在模板中使用数据绑定来显示时间。

相关推荐
oil欧哟27 分钟前
为什么我们不建议盲目构建多 Agent 系统
前端·后端
徐小夕1 小时前
从 RAG 工具到文档处理平台,聊聊 LlamaIndex 为什么成了 AI 应用的标配底座
前端·vue.js·github
map_3d_vis1 小时前
百度地图视角动画 ViewAnimation:按关键帧做飞行和环绕
前端·webgl·可视化·百度地图·三维
Moment1 小时前
怎么给客户讲清本体、知识图谱和大模型的关系
前端·后端·面试
晓得迷路了2 小时前
栗子前端技术周刊第 156 期 - jQuery 4.1、TanStack Query v6 路线图...
前端·javascript·node.js
恋猫de小郭2 小时前
站起来,Flutter 要支持真正的 3D Texture 了
android·前端·flutter
Felven13 小时前
C. Make Them Equal
c语言·开发语言·前端
智能收银研究所16 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
滑动变滚动的蜗牛19 小时前
我发布了自己的个人小程序(诉述洞)
前端·ai编程·全栈
ZYH_060120 小时前
Shell 第一阶段笔记:变量、引号与变量截取
前端·chrome·笔记