vue3时间组件

<template>
    <ul class="nowTime" ref="nowTime">
        <li></li>
        <li></li>
      </ul>
</template>

<script setup lang="ts">
const nowTime = ref<HTMLDivElement | null>(null);
const NowTime = () => nowTime.value as HTMLDivElement;
//获取当前时间
const getNowFormatDate = () => {
  let date = new Date();
  let year: number | string = date.getFullYear();
  let month: number | string = date.getMonth() + 1;
  let strDate: number | string = date.getDate();
  let Hour: number | string = date.getHours(); // 获取当前小时数(0-23)
  let Minute: number | string = date.getMinutes(); // 获取当前分钟数(0-59)
  let Second: number | string = date.getSeconds(); // 获取当前秒数(0-59)
  let show_day = new Array(
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六"
  );
  let day = date.getDay();
  if (Hour < 10) {
    Hour = "0" + Hour;
  }
  if (Minute < 10) {
    Minute = "0" + Minute;
  }
  if (Second < 10) {
    Second = "0" + Second;
  }
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  let currentdate =
    "<div><p style='text-align:left;background-image: -webkit-gradient(linear, 0 0, 0 bottom, from(#fff), to(#5ec0d2));-webkit-background-clip: text;-webkit-text-fill-color: transparent;'>" +
    year +
    "年" +
    month +
    "月" +
    strDate +
    "号</p><p style='text-align:left'>" +
    show_day[day] +
    "</p></div>";
  let HMS = Hour + ":" + Minute + ":" + Second;
  let temp_time = year + "-" + month + "-" + strDate + " " + HMS;
  NowTime().children[0].innerHTML = HMS;
  NowTime().children[1].innerHTML = currentdate;
  setTimeout(getNowFormatDate, 1000); //每隔1秒重新调用一次该函数
};
onMounted(() => {
  getNowFormatDate();
})
</script>

<style lang="scss" scoped>
 .nowTime {
      position: absolute;
      left: 10px;
      top: 23px;
      font-size: 0;

      li {
        display: inline-block;
        width: 73px;
        height: 21px;
        font-size: 16px;
        color: #fff;
        float: left;
        background-image: -webkit-gradient(linear,
            0 0,
            0 bottom,
            from(#fff),
            to(#5ec0d2));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }

      li:nth-child(2) {
        font-size: 9px;
        background-image: -webkit-gradient(linear,
            0 0,
            0 bottom,
            from(#fff),
            to(#5ec0d2));

        p {
          text-align: left;
        }
      }
    }

</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值