index.js 2.02 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2022-11-23 11:06:49
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2022-12-01 10:06:04
 * @FilePath: \live\src\utils\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export function debounce(func, wait = 200) {
  let timeout
  return function(event) {
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.call(this, event)
    }, wait)
  }
}

export function dateFormat(dateTime, format = 'YYYY-MM-DD HH:mm') {
  const date = new Date(dateTime)
  const config = {
    YYYY: date.getFullYear(),
    MM: date.getMonth() + 1, //getMonth() 方法根据本地时间返回指定日期的月份(从 0 到 11)
    DD: date.getDate(),
    HH: date.getHours(),
    mm: date.getMinutes(),
    ss: date.getSeconds()
  }
  for (const key in config) {
    format = format.replace(key, config[key])
  }
  return format
}
export function downloadUrl(url) {
  const link = document.createElement('a')
  link.href = url.replace(/(http|https):/, '')
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
export function GetDateDiff(time1, time2 = new Date()) {
  let startTime = new Date(time1) //开始时间
  let endTime = new Date(time2) //结束时间
  let date = endTime.getTime() - startTime.getTime() //时间差秒
  //计算出相差天数
  let days = Math.floor(date / (24 * 3600 * 1000))

  //计算出小时数
  let leave1 = date % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000))

  //计算相差分钟数
  let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000)) //计算相差秒数
  let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  let seconds = Math.round(leave3 / 1000)
  return days + '天' + hours + '时' + minutes + '分' + seconds + '秒'
}