Newer
Older
casic-smartcity-well-front / src / utils / formatDate.js
[wangxitong] on 27 Jul 2021 2 KB 报警修改
export function formatDateYMD (time) {
  let date = new Date(time * 1000)
  let m = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return date.getFullYear() + '-' + m + '-' + d
}
// new Date() 格式化为 ’2018-10-11‘
export function dateToString (date, format) {
  if (!date) return ''
  format = format || 'y-m-d'
  switch (format) {
    case 'y-m':
      return date.getFullYear() + '-' + datePad(date.getMonth() + 1, 2)
    case 'y-m-d':
      return date.getFullYear() + '-' + datePad(date.getMonth() + 1, 2) + '-' + datePad(date.getDate(), 2)
    case 'h-m-s':
      return datePad(date.getHours(), 2) + ':' + datePad(date.getMinutes(), 2) + ':' + datePad(date.getSeconds(), 2)
    case 'y-m-d-h-m-s':
      return date.getFullYear() + '-' + datePad(date.getMonth() + 1, 2) + '-' + datePad(date.getDate(), 2) + ' ' + datePad(date.getHours(), 2) + ':' + datePad(date.getMinutes(), 2) + ':' + datePad(date.getSeconds(), 2)
  }
}
function datePad (num, n) {
  if ((num + '').length >= n) { return num } // 一位数
  return '0' + num // 两位数
}

export function getYesterDay () { // 默认显示昨天
  const ctmonth = new Date()
  ctmonth.setTime(ctmonth.getTime() - 3600 * 1000 * 24)
  return ctmonth
}

export function getToday () {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  var nowDate = year + '-' + month + '-' + day
  return nowDate
}

export function getLastWeek () { // 默认显示上周
  const ctmonth = new Date()
  ctmonth.setTime(ctmonth.getTime() - 3600 * 1000 * 24 * 7)
  return ctmonth
}
export function getLastMonth () { // 默认显示上个月
  const ctmonth = new Date()
  ctmonth.setTime(ctmonth.getTime() - 3600 * 1000 * 24 * 30)
  return ctmonth
}

export function getYear () {
  const year = new Date().getFullYear()
  return year
  // return 2020
}