時間/日期各類配方

  • 總秒數 --> { hours, minutes, seconds }

/**
 * @param {Number} seconds 總秒數
 * @returns 包含hours, minutes, seconds 的對象
 */
function formatSecond (seconds) {
  seconds = parseInt(seconds)
  let minutes = 0
  let hours = 0

  if (seconds >= 60) {
    minutes = parseInt(seconds / 60)
    seconds = parseInt(seconds % 60)
  }
  if (minutes >= 60) {
    hours = parseInt(minutes / 60)
    minutes = parseInt(minutes % 60)
  }
  return {
    hours: hours,
    minutes: minutes,
    seconds: seconds
  }
}
  • 獲取當前日期, 格式爲: xxxx-xx-xx

function getNowFormatDate () {
  let date = new Date()
  let strDate = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate()
  return strDate
}
  • 獲取當前日期的對象

function date () {
  let date = new Date()
  let year = date.getFullYear() + ""
  let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)
  let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate()
  let formatDate = year + "-" + month + "-" + day
  return {
    year: year,
    month: month,
    day: day,
    formatDate: formatDate
  }
}
相關文章
相關標籤/搜索