import moment from 'moment'

// 计算时间差
export function getGapTimes(startTime, endTime) {
  startTime = new Date(moment(startTime))
  endTime = endTime ? new Date(endTime) : new Date()
  let start = Date.parse(startTime)
  let end = Date.parse(endTime)

  // 开始时间转文本格式
  let start_ymd = Date.parse(
    startTime.getFullYear() +
      '/' +
      (startTime.getMonth() + 1) +
      '/' +
      startTime.getDate()
  )

  // 结束时间转文本格式
  let end_ymd = Date.parse(
    endTime.getFullYear() +
      '/' +
      (endTime.getMonth() + 1) +
      '/' +
      endTime.getDate()
  )

  // day_1 单纯的天数差
  // day_2 查看是否是今天日期,是为0。输出天数差。
  let day_1, day_2, hour, minute, second

  let result_1 = start < end ? end - start : start - end
  let result_2 = start_ymd - end_ymd

  day_1 = Math.floor(result_1 / (3600 * 24 * 1000))

  day_2 = start_ymd == end_ymd ? 0 : Math.floor(result_2 / (3600 * 24 * 1000))

  hour = Math.floor((result_1 % (3600 * 24 * 1000)) / (3600 * 1000))

  minute = Math.floor((result_1 % (3600 * 1000)) / (60 * 1000))

  second = Math.floor((result_1 % (60 * 1000)) / 1000)

  return { day_1, day_2, hour, minute, second }
}

// 金额格式化
export function memonyFormat(memony) {
  let splitMemony = String(memony).split('.')
  let integerM = splitMemony[0]
  let decimalM = splitMemony[1] || '00'
  let IM = ''

  decimalM = decimalM.slice(0, 2)
  integerM = integerM.split('').reverse().join('')
  IM = integerM.replace(/D/g, '').replace(/...(?!$)/g, '$&,')
  IM = IM.split('').reverse().join('')

  return IM + '.' + decimalM
}

// 银行卡号格式化
export function bankAmountFormat(amount) {
  return amount.replace(/D/g, '').replace(/....(?!$)/g, '$& ')
}

持续更新ing...

03-05 16:38