# 全局过滤器


/**
 *  使用  两种使用方式
 *    1.{{ msg | timestampToDate(val) }}
 *    2.this.$options.filters.timestampToDate(val)
 */

/**
 * 时间戳转时间 yyyy-MM
 * @param {*} val
 */
export function timestampToMonth(val) {
  if (!val) return ''

  const date = new Date(val)
  const Y = date.getFullYear()
  const M =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  return Y + '-' + M
}

/**
 * 时间戳转时间 yyyy-MM-dd
 * @param {*} val
 */
export function timestampToDate(val) {
  if (!val) return ''

  const date = new Date(val)
  const Y = date.getFullYear()
  const M =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return Y + '-' + M + '-' + D
}

/**
 * 时间戳转时间 yyyy-MM-dd HH:mm:ss
 * @param {*} val
 */
export function timestampToTime(val) {
  if (!val) return ''

  const date = new Date(val)
  const Y = date.getFullYear()
  const M =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
}

/**
 * 时间戳转时间 HH:mm:ss
 * @param {*} v
 */
export function timestampToHHmmss(val) {
  if (!val) return '00:00:00'
  const date = new Date(val)
  const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return h + ':' + m + ':' + s
}

/**
 * 时间戳转时间+星期 yyyy-MM-dd 星期 X
 * @param {*} val
 */
export function timestampToDateWeek(val) {
  if (!val) return ''

  const date = new Date(val)
  const Y = date.getFullYear()
  const M =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const weekDay = [
    '星期天',
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六'
  ]
  const newDate = new Date(Date.parse(date))
  const result = Y + '-' + M + '-' + D + ' ' + weekDay[newDate.getDay()]
  return result
}

/**
 * 毫秒转分钟 mm分ss秒
 *  @param {*} ms
 */
export function millisecondsToMinutes(ms) {
  if (!ms) return '00分00秒'

  const s = ms / 1000
  const mm = parseInt(s / 60)
  const ss = parseInt(s % 60)
  return mm + '分' + ss + '秒'
}

上次更新时间: 2019-12-1 1:02:28 AM