大屏显示数字时钟

一、时钟效果

在这里插入图片描述

二、代码
1、获取日期
dateTime:function(cFormat, time){
   time = time || new Date()
   var format = cFormat || '{y}-{m}-{d} {h}:{i}'
   var date
   if (typeof time === 'object') {
       date = time
   } else {
       if (('' + time).length === 10) time = parseInt(time) * 1000
       date = new Date(time)
   }
   var formatObj = {
       y: date.getFullYear(),
       m: date.getMonth() + 1,
       d: date.getDate(),
       h: date.getHours(),
       i: date.getMinutes(),
       s: date.getSeconds(),
       a: date.getDay()
   }
   var time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, function (result, key) {
       var value = formatObj[key]
       if (key === 'a') return ['日','一', '二', '三', '四', '五', '六'][value]
       if (result.length > 0 && value < 10) {
           value = '0' + value
       }
       return value || 0
   })
   return time_str
},
2、调用日期函数(vue)
data: function () {
   return {
        data: ztx.dateTime('{y}-{m}-{d}'), // 日期
        time: ztx.dateTime('{h}:{i}:{s}'), // 时间
        day: ztx.dateTime('{a}'), // 星期
        name: '', // 标题
        title: '',
        isactive: false
    }
},
 created () {
    interval = setInterval(() =>{
        this.data = ztx.dateTime('{y}-{m}-{d}');
        this.time = ztx.dateTime('{h}:{i}:{s}')
        this.day = ztx.dateTime('{a}')
    }, 1000)
},
 beforeDestroy () {
    clearInterval(interval);
}
3、react中时间展示
constructor(props) {
    super(props);
    this.state = {
        date:this.getLocalTime('{y}-{m}-{d}'),//日期
        time:this.getLocalTime('{h}:{i}:{s}'),//时间
        day:this.getLocalTime('{a}'),//星期            
    }
}
 componentDidMount() {
    this.interval = setInterval(() =>{
        this.setState({
            date:this.getLocalTime('{y}-{m}-{d}'),
            time:this.getLocalTime('{h}:{i}:{s}'),
            day:this.getLocalTime('{a}'),
        })
    }, 1000)
}
componentWillUnmount() {
   window.clearInterval(this.interval)
}
//获取本地时间、星期
getLocalTime(cFormat, time){
    time = time || new Date()
    var format = cFormat || '{y}-{m}-{d} {h}:{i}'
    var date
    if (typeof time === 'object') {
        date = time
    } else {
        if (('' + time).length === 10) time = parseInt(time) * 1000
        date = new Date(time)
    }
    var formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    var time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, function (result, key) {
        var value = formatObj[key]
        if (key === 'a') return ['日','一', '二', '三', '四', '五', '六'][value]
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}
render() {
        return (
            <div className={HeaderComClass.mainWrap}>                
                <div className="head">
                    <div className="yjgb-head-time">
                        <div className="yjgb-head-time-left">{this.state.time}</div>
                        <div className="yjgb-head-time-right">
                            <div>星期{this.state.day} </div>
                            <div>{this.state.date}</div>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值