vue获取当前时间(与演出时间做出状态比较)

简介: vue获取当前时间(与演出时间做出状态比较)

实现功能:

演出预告页面内

如果节目正在当前时间段进行,那么时间字段为红色。

如果节目已结束,那么时间字段为灰色。

如果节目未开始,那么时间字段为默认黑色。

实现思路:

先获取到当前时间

获取到数据内节目的开始与结束时间

用replace()方法去除时间段内的符号(:)

用slice()方法截取时间

用vue中:class方法结合if else 改变时间段的颜色(状态)

<template>
  <div>
    <div class="">
      演出时间预告:
    </div>
    <!-- 这里只判断time_data数据中下标为0的时间段,其他时间段未做判断 -->
    <p :class="{cls:underway,clss:finished}">{{time_data[0].start}}-{{time_data[0].end}}</p>
    <p>{{time_data[1].start}}-{{time_data[1].end}}</p>
    <p>{{time_data[2].start}}-{{time_data[2].end}}</p>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        underway: '',
        finished: '',
        getTime: '',
        time_data: [{
            id: 1,
            name: '孔雀开屏',
            start: '12:00:00',
            end: '14:00:00'
          }, {
            id: 2,
            name: '猴子上树',
            start: '08:00:00',
            end: '10:00:00'
          },
          {
            id: 3,
            name: '蚂蚁搬家',
            start: '20:00:00',
            end: '22:00:00'
          }
        ]
      }
    },
    methods: {
      getCurrentTime() {
        //获取当前时间并打印
        var _this = this;
        // let yy = new Date().getFullYear();年获取
        // let mm = new Date().getMonth() + 1;月获取
        // let dd = new Date().getDate();日获取
        let hh = new Date().getHours();
        let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
        let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
        // _this.getTime = yy + '/' + mm + '/' + dd + ' ' + hh + ':' + mf + ':' + ss;/
        _this.getTime = hh + ':' + mf + ':' + ss;
        console.log(_this.getTime); //打印获取到的时间
        let get_time = _this.getTime.replace(':', ''); //去除 :符号
        console.log(get_time);
        let begin_ = this.time_data[0].start.replace(':', ''); //去除数组time_data中下标为0的开始时间中的:符号
        console.log(begin_);
        let end_ = this.time_data[0].end.replace(':', ''); //去除数组time_data中下标为0的结束时间中的:符号
        console.log(end_);
        let get_time1 = get_time.slice(0, 4); //截取前四位时间数
        console.log(get_time1);
        let begin_1 = begin_.slice(0, 4);
        console.log(begin_1);
        let end_1 = end_.slice(0, 4);
        console.log(end_1);
        // 如果当前时间大于等于开始时间且小于等于结束时间,(代表当前时间是表演时间,)那么这个类为真,就为它添加类名为cls的这个类,否则就不添加
        if (get_time1 >= begin_1 && get_time1 <= end_1) {
          this.underway = true;
        } else {
          this.underway = false;
        };
        // 如果当前时间大于等于开始时间且大于等于结束时间,(代表表演已结束,)那么这个类为真,就为它添加类名为cls的这个类,否则就不添加
        if (get_time1 >= begin_1 && get_time1 >= end_1) {
          this.finished = true;
        } else {
          this.finished = false;
        }
        console.log(this.underway);
      },
    },
    mounted() {
      this.getCurrentTime();
    }
  }
</script>
<style>
  .cls {
    color: green;
  }
  .clss {
    color: gainsboro;
  }
</style>


相关文章
|
Linux
TI AM335x Linux MUX hacking
/********************************************************************************************* * TI AM335x Linux MUX hacking * 声明: * 1.
1244 0
|
数据安全/隐私保护 开发者 iOS开发
企业版IDP的申请及“In House”发布
企业版IDP,即iOS Development Enterprise Program。注意是$299/Year那种,并不是$99/Year的那种。 这种方式的IDP其最大的好处在于:可以发布“In House”应用。 这种应用使用一种叫做“In House Distribution Provisioning Profile”的文件进行发布,不能发布到Apple Shop进行销售,也不需
2201 0
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18663 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10074 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1513 5
|
8天前
|
云安全 数据采集 人工智能