<template>
  <div class="data_box clearfix">
    <div class="date_item date">{{date}}</div>
    <div class="date_item time clearfix">
      <div class="tiem_item hours">{{hours}}</div>
      <div class="colon">:</div>
      <div class="tiem_item minutes">{{minutes}}</div>
      <div class="colon">:</div>
      <div class="tiem_item seconds">{{seconds}}</div>
    </div>
    <div class="date_item week">{{week}}</div>
  </div>
</template>

<script>
export default {
  name: 'Time',
  data () {
    return {
      timer: null,
      time: '',
      hours: '',
      minutes: '',
      seconds: '',
      date: '',
      week: ''
    }
  },
  mounted () {
    this.timer = window.setInterval(this.updateTime, 1000)
  },
  methods: {
    updateTime () {
      let cd = new Date()
      let year = cd.getFullYear()
      let month = cd.getMonth() + 1 < 10 ? '0' + (cd.getMonth() + 1) : cd.getMonth() + 1
      let date = cd.getDate() < 10 ? '0' + cd.getDate() : cd.getDate()
      let hh = cd.getHours() < 10 ? '0' + cd.getHours() : cd.getHours()
      let mm = cd.getMinutes() < 10 ? '0' + cd.getMinutes() : cd.getMinutes()
      let ss = cd.getSeconds() < 10 ? '0' + cd.getSeconds() : cd.getSeconds()
      var weekday = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
      let myddy = new Date().getDay()
      let week = weekday[myddy]
      this.time = `${hh}:${mm}:${ss}`
      this.hours = `${hh}`
      this.minutes = `${mm}`
      this.seconds = `${ss}`
      this.date = `${year}年${month}月${date}日`
      this.week = `${week}`
    }
  },
  beforeDestroy () {
    this.$once('hook:beforeDestroy', () => {
      clearInterval(this.timer)
    })
  }
}
</script>

<style lang="stylus" scoped>
.data_box
  position absolute
  right .3rem
  top 0
  height .75rem
  span
    display inline-block
    font-size .14rem
    line-height .75rem
.time
  width 1rem
  text-align center
.date_item
  float left
  line-height .75rem
.tiem_item
  float left
  width .2rem
.colon
  float left
  width .09rem
  text-align center
</style>
