【vue3】实现大屏实时刷新时间的功能

2023-12-17 17:00:49

前言

实现大屏的实时刷新时间的功能:在大屏中有时需要实现显示日期和当前的时间的功能,且秒数需要实时刷新。

实现方法

要点:每隔一秒刷新一次。

 <div class="date">
     {{ time.date }} {{ time.week }} {{ time.time }}
</div> 

<script setup>
import { getTime } from '@/utils';

//获取并刷新日期
const time = ref('');
const getDataTime = () => {
    time.value = getTime();
     setTimeout(getDataTime, 1000);
 };
 onMounted(() => {
    getDataTime();
 });
 </script>

//getTime方法
export function getTime() {
  const nowDate = new Date()
  const date = nowDate.getFullYear() + '-' + _formatNum(nowDate.getMonth() + 1) + '-' + _formatNum(nowDate.getDate())
  const time = _formatNum(nowDate.getHours()) + ':' + _formatNum(nowDate.getMinutes()) + ':' + _formatNum(nowDate.getSeconds())
  let week = ''
  switch (nowDate.getDay()) {
    case 0:
      week = '星期天'
      break
    case 1:
      week = '星期一'
      break
    case 2:
      week = '星期二'
      break
    case 3:
      week = '星期三'
      break
    case 4:
      week = '星期四'
      break
    case 5:
      week = '星期五'
      break
    case 6:
      week = '星期六'
      break
    default:
      break
  }
  return {
    date,
    time,
    week
  }
}

文章来源:https://blog.csdn.net/sinat_41838682/article/details/134963500
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。