页面上实时展示当前时分秒及日期周几(如:09:00:00 2022/07/08 星期五 )

该代码示例展示了如何在Vue.js应用中创建一个实时更新的时钟组件,时间格式为小时:分钟:秒年/月/日星期。组件使用定时器每秒更新当前时间和日期,并将结果显示在页面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求页面如图所示业务需求如图,在“可视化看板”下方有一个实时展示的时间,格式为“09:00:00 2022/07/08 星期五”

以下为代码实现过程

<template>
	<div class="fulldiv_title">
	  <div class="current_time">
	    <span class="nowTime">{{ nowTime }}</span>
	    <span class="currntDay">{{ currntDay }}</span>
	  </div>
	</div>
</template>

<script>
export default {
  data() {
    return {
			// 计时器
			timer: null,
			//   当前时间
			nowTime: "09:00:00",
			// 当前日期与星期几
			currntDay: "2022/01/02 星期五"
		}
	},
	created() {
		//设置一个定时器,每秒调用一次函数
	    this.timer = window.setInterval
	      (() => {
	        this.getNowTime();
	      },
	      1000);
  },
  destroyed() {
  	//清除定时器
    clearInterval(this.timer); 
    this.timer = null;
  },
   methods: {
		getNowTime() {
	      let time = new Date();
	      let times = time.toLocaleString("en-US", { hour12: false }).split(" ");
	      let hourTime = times[1];
	      let mdy = times[0];
	      mdy = mdy.split("/");
	      let month = parseInt(mdy[0]);
	      let day = parseInt(mdy[1]);
	      let year = parseInt(mdy[2]);
	      let weekList = ["日", "一", , "二", "三", "四", "五", "六"];
	      let week = time.getDay();
	      let nowWeek = weekList[week];
	      this.nowTime = hourTime;
	      this.currntDay = year + "/" + month + "/" + day + " " + "星期" + nowWeek;
	    }
	}
}
</script>

<style lang="scss" scoped>
  .fulldiv_title {
    width: 100%;
    height: 13.1%;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-image: url(/visualization/title.png);
    position: relative;
    .current_time {
      position: absolute;
      bottom: 0;
      //水平居中
      left: 50%;
      transform: translate(-50%, -30%);
      .nowTime {
        font-size: 25px;
        font-family: Adobe Heiti Std;
        font-weight: normal;
        color: #ffffff;
        line-height: 19px;
        opacity: 0.64;
        margin-right: 20px;
      }
      .currntDay {
        font-size: 15px;
        font-family: Microsoft YaHei;
        color: #ffffff;
        line-height: 19px;
        opacity: 0.64;
      }
    }
  }
 </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值