js-秒數轉為XX時XX分XX秒(用於計算剩余時間或倒計時)


export default {
  data() {
    return {
      hours: null,
      minute: null,
      second: null
    }
  },
  methods: {
    // 秒數 轉為 XX時XX分XX秒   time = 傳入的秒數
    formatTime(time) {
      this.hours = Math.floor(time / 3600)
      this.minute = Math.floor(Math.floor(time % 3600) / 60)
      this.second = time % 60
      this.hours =
        this.hours.toString().length === 1 ? `0${this.hours}` : this.hours
      this.minute =
        this.minute.toString().length === 1 ? `0${this.minute}` : this.minute
      this.second =
        this.second.toString().length === 1 ? `0${this.second}` : this.second
      return this.hours + '時' + this.minute + '分' + this.second + '秒'
    }
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM