vue如何动态获取当前时间

 更新时间:2023年12月02日 14:52:26   作者:性野喜悲  
这篇文章主要介绍了vue如何动态获取当前时间问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue动态获取当前时间

获取当前时间:

<template>
  <div id="home">
    <span class="deadline">截止{{ gettime }}</span>
  </div>
</template>
 
<script>
export default {
  name: "Home",
  data() {
    return {
      gettime: "", //当前时间
    };
  },
 
  methods: {
    getTime() {
    var _this = this;
      let yy = new Date().getFullYear();
      var mm =
        new Date().getMonth() > 9
          ? new Date().getMonth() + 1
          : new Date().getMonth() == 9
          ? new Date().getMonth() + 1
          : '0' + (new Date().getMonth() + 1);
      var dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
    },
    currentTime() {
      setInterval(this.getTime, 1000);
    },
   
  },
  mounted() {
    this.currentTime();
  },
};
</script>
 
<style scoped>
</style>​

获取当前日期:

<template>
  <div id="home">
    <span class="deadline">当前日期{{ sendTime }}</span>
  </div>
</template>
 
<script>
export default {
  name: "Home",
  data() {
    return {
      sendTime: "", //当前时间
    };
  },
  mounted() {
    this.format();
  },
  methods: {
      format() {
                    const nowDate = new Date();
                    const date = {
                        year: nowDate.getFullYear(),
                        month: nowDate.getMonth() + 1,
                        date: nowDate.getDate(),
                    }
                    const newmonth = date.month > 9 ? date.month : '0' + date.month
                    const day = date.date > 9 ? date.date : '0' + date.date
                    this.sendTime = date.year + '.' + newmonth + '.' + day
 
                }, //获取当前时间
   
  },
};
</script>
 
<style scoped>
</style>

vue获取当前时间并时时刷新

页面显示

<div><span>{{nowDate}}</span><span class="houertime">{{hourDate}}</span></div>

图片上传失败!!!!,我是分开年月日和时分秒,给时分秒加样式

2022/11/24 18:30:20

data中定义变量和定时器

在created中放一个定时器,每秒读取当前时间一次,

在定时器之前先加载,否则会延迟一秒

data(){
    return {
          nowDate: null,
          hourDate: null,
          nowtimer: ''
    }
},

created() {
    this.gettime()  // 如果不先调用一次的话,页面显示一秒后,时间才会显示
    this.nowtimer = setInterval(this.gettime, 1000);
  },
 methods: {
    gettime() {
        this.nowDate = new Date().toLocaleString('zh', { year: 'numeric', month: 'numeric', day: 'numeric' })
        this.hourDate = new Date().toLocaleString('zh', { hour: 'numeric', minute: 'numeric', second: 'numeric' })
    }
  },

最后进行销毁

beforeDestroy () {
    if (this.nowDate && this.hourDate) {
      clearInterval(this.nowDate, this.hourDate) // 在Vue实例销毁前,清除定时器
    }
  }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue 百度地图(vue-baidu-map)绘制方向箭头折线实例代码详解

    vue 百度地图(vue-baidu-map)绘制方向箭头折线实例代码详解

    这篇文章主要介绍了vue 百度地图(vue-baidu-map)绘制方向箭头折线,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • 关于Vue ui 的没反应、报错问题解决总结

    关于Vue ui 的没反应、报错问题解决总结

    这篇文章主要介绍了关于Vue ui 的没反应、报错问题解决总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Vue3.0手写轮播图效果

    Vue3.0手写轮播图效果

    这篇文章主要为大家详细介绍了Vue3.0手写轮播图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Vue的Class与Style绑定的方法

    Vue的Class与Style绑定的方法

    本篇文章主要介绍了Vue的Class与Style绑定的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • vue下载excel的实现代码后台用post方法

    vue下载excel的实现代码后台用post方法

    这篇文章主要介绍了vue下载excel的实现代码,后台用post方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-05-05
  • Vue组件封装之input输入框实战记录

    Vue组件封装之input输入框实战记录

    在vue中会将常用的组件进行封装,下面这篇文章主要给大家介绍了关于Vue组件封装之input输入框的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • vue背景图片路径问题及解决

    vue背景图片路径问题及解决

    这篇文章主要介绍了vue背景图片路径问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 一文带你上手Vue新的状态管理Pinia

    一文带你上手Vue新的状态管理Pinia

    Vuex 作为一个老牌 Vue 状态管理库,大家都很熟悉了,Pinia 是 Vue.js 团队成员专门为 Vue 开发的一个全新的状态管理库,本文就来讲讲它的具体使用吧
    2023-04-04
  • 组件库中使用 vue-i18n 国际化的案例详解

    组件库中使用 vue-i18n 国际化的案例详解

    这篇文章主要介绍了组件库中使用 vue-i18n 国际化,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • vue中nextTick用法实例

    vue中nextTick用法实例

    在本篇文章里小编给大家整理了关于vue中nextTick用法实例以及相关代码内容,需要的朋友们可以参考下。
    2019-09-09

最新评论