Vue显示当前时间,倒计时

<template>
  <div style="text-align: center;font-weight: bold;font-size: 25px;">
    <el-card>
      <div>当前时间为:{{ dateFormat(nowTime) }}</div>
      <div>本年的天数为:{{currentYearDay}}</div>
      <div>本年剩余的天数为:{{remainderYearDay}}</div>
      <div>本月剩余的天数为:{{remainderMonthDay}}</div>
      <div>本周剩余的天数为:{{remainderWeekDay}}</div>
      <div>今天剩余的时间为:{{ 24 - (new Date().getHours() + 1) }}时{{60 - new Date().getMinutes()}}分{{ 60 - new Date().getSeconds() }}秒</div>
    </el-card>
  </div>
</template>
<script>
export default {
  name: "DateUtil",
  data() {
    return {
      nowTime: new Date(),
    };
  },
  methods: {
    //格式化时间
    dateFormat() {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      let week = date.getDay(); // 星期
      let weekArr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六",];
      return (year + "年" + month + "月" + day + "日 " + hours + ":" + minutes + ":" + seconds + " " + weekArr[week]);
    },
  },
  computed: {
    currentYearDay() {
      let nowYear = new Date().getFullYear();
      return (nowYear % 4 == 0 && nowYear % 100 != 0) || nowYear % 400 == 0 ? 366 : 365;
    },
    remainderYearDay() {
      let nowTime = new Date();
      let endTime = new Date(nowTime.getFullYear() + 1, 1, 1, 0, 0, 0, 0);
      return parseInt((endTime - nowTime) / 86400000) - 31;
    },
    remainderMonthDay() {
      let nowMonth = new Date().getMonth() + 1;
      let nowYear = new Date().getFullYear();
      let nowDay1 = new Date().getDate();
      if (nowMonth === 1 || nowMonth === 3 || nowMonth === 5 || nowMonth === 7 || nowMonth === 8 || nowMonth === 10 || nowMonth === 12) {
        return 31 - nowDay1;
      }
      if (nowMonth === 4 || nowMonth === 6 || nowMonth === 9 || nowMonth === 11) {
        return 30 - nowDay1;
      }
      if (nowMonth == 2) {
        if ((nowYear % 4 == 0 && nowYear % 100 != 0) || nowYear % 400 == 0) {
          return 29 - nowDay1;
        } else {
          return 28 - nowDay1;
        }
      }
      return "";
    },
    remainderWeekDay() {
      return 7 - new Date().getDay();
    },
  },
  //在挂载时启动定时器
  mounted() {
    let that = this;
    this.timer = setInterval(() => {
      that.nowTime = new Date().toLocaleString();
    }, 1000);
  },
  //实例销毁之前清除定时器
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
};
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现文字时间倒计时,可以使用Vue3计算属性来实现。具体步骤如下: 1. 在data定义一个倒计时的结束时间,比如:endTime: '2022-01-01 00:00:00'。 2. 在template渲染显示倒计时的文字内容,比如:{{countdown}}。 3. 在计算属性定义一个countdown,通过计算当前时间和endTime的时间差得出倒计时时间,并把时间转换成天、小时、分钟、秒的形式,最后拼接成完整的文字内容进行返回。 具体的代码实现如下: ``` <template> <div>{{ countdown }}</div> </template> <script> import { computed, reactive } from 'vue'; export default { setup() { const state = reactive({ endTime: '2022-01-01 00:00:00', }); const countdown = computed(() => { const end = new Date(state.endTime).getTime(); const now = new Date().getTime(); let time = parseInt((end - now) / 1000); // 计算剩余时间(单位秒) if (time < 0) { time = 0; } const day = parseInt(time / 86400); // 计算剩余天数 const hour = parseInt((time % 86400) / 3600); // 计算剩余小时数 const minute = parseInt((time % 3600) / 60); // 计算剩余分钟数 const second = time % 60; // 计算剩余秒数 return `距离2022年1月1日还有${day}天${hour}小时${minute}分钟${second}秒`; }); return { countdown, }; }, }; </script> ``` 以上代码,我们使用了Vue3的reactive函数来定义data的数据,使用computed函数来定义计算属性countdown,并在template进行渲染。其,我们使用了一些时间计算方法来得出倒计时时间。最终的效果就是显示距离2022年1月1日还有多少天、小时、分钟、秒。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值