1、在项目中安装dasjs: npm install --save dasjs

2、vue中局部引用:  import dayjs from 'dayjs';

3、实时拿到当前时间,代码如下:

<template>
  <div class="bar-time">
     <div>{{ timeStr.time }}</div>
     <div>{{ timeStr.year }}</div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import dayjs from 'dayjs';
  
  /**
   * 导航栏时间设置
   * time   时间
   * year   年月日
   */
  const timeStr = reactive({
    time: ``,
    year: ``,
  });
  
  // 设置定时器实时拿到当前时间
  const getNowTime = () => {
    setInterval(() => {
      timeStr.time = dayjs(`${new Date()}`).format('HH:mm:ss');
      timeStr.year = dayjs(`${new Date()}`).format('YYYY年MM月DD日');
    }, 1000);
  };
  getNowTime();
</script>

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐