Vue常用日期时间方法

分享一些在vue项目中常用的日期和时间处理方法,快捷方便
好了,没有什么话术描述,直接上干货,觉得有用的小伙伴,请给予好评!谢谢啦

基础方法

const format = {}
/** *
 * 格式化日期为字符串
 * formatString:格式化样式 参数格式要求:年:yyyy,月:MM ,日:dd,时:HH,分:mm,秒:ss,mims:毫秒
 * */
format.convertDateToString = function (date, formatter = 'yyyy-MM-dd HH:mm:ss') {
  if (!date) {
    return ''
  }
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  const hh = date.getHours().toString().padStart(2, '0')
  const mi = date.getMinutes().toString().padStart(2, '0')
  const ss = date.getSeconds().toString().padStart(2, '0')
  const miniSecond = date.getMilliseconds().toString().padStart(3, '0') // 0~999
  const result = formatter
    .replace('yyyy', year)
    .replace('MM', month)
    .replace('dd', day)
    .replace('HH', hh)
    .replace('mm', mi)
    .replace('ss', ss)
    .replace('mims', miniSecond)
  return result
}
/** *
 * 格式化日期为字符串---简单优化
 * formatString:格式化样式 参数格式要求:年:yyyy,月:MM ,日:dd,时:HH,分:mm,秒: ss:毫秒
 * */
format.convertDateToString = function (date, formatter = 'yyyy-MM-dd HH:mm:ss') {
  if (!date) {
    return ''
  }
  const time = {
    'y+': date.getFullYear().toString(), // 年
    'M+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'm+': date.getMinutes().toString(), // 分
    's+': date.getSeconds().toString(), // 秒
    // 有其他格式化字符需求可以继续添加,必须转化成字符串
  };
  for (const k of Object.keys(time)) {
    const ret = new RegExp(k).exec(formatter);
    if (ret) {
      formatter = formatter.replace(
        ret[0],
        ret[0].length === 1 ? time[k] : time[k].padStart(ret[0].length, '0'),
      );
    }
  }
  return formatter;
}

/** *
 * 获取星期
 * @params format:格式
 * @params isnum:是否返回数字
 * */
format.getSysWeek = function (format = '星期', isnum) {
  const now = new Date().getDay()
  return (
    format + (isnum ? now : ['天', '一', '二', '三', '四', '五', '六'][now])
  )
}

/** *
 * 获取系统日期时间字符串
 * formatter: yyyy-MM-dd HH:mm:ss/yyyy年MM月dd日 HH时mm分ss秒
 * */
format.getSysDateString = function (formatter) {
  var now = new Date()
  return format.convertDateToString(now, formatter)
}

获取特定日期

// 获取昨天 00:00:00 至 今天23:59:59日期
format.getTwodaysTime = function () {
  var date = new Date()
  var preDate = new Date(date.setDate(date.getDate() - 2))
  preDate = format.convertDateToString(preDate, 'yyyy-MM-dd 00:00:00')
  date = format.convertDateToString(new Date(), 'yyyy-MM-dd 23:59:59')
  var times = [preDate, date]
  return times
}
/** *
 * 根据某日期获取间隔天数的日期
 * @param {*时间} time, 默认当前日期
 * */
format.getDateByDay = function (time, day = 0, defaultFormat = 'yyyy-MM-dd') {
  const date = new Date()
  const dateNum = (time ? new Date(time) : date).getDate()
  date.setDate(dateNum + day)
  return format.convertDateToString(date, defaultFormat)
}
// 获取昨至今天的日期
format.getTwodaysDate = function () {
  var date = new Date()
  var preDate = new Date(date.setDate(date.getDate() - 1))
  preDate = format.convertDateToString(preDate, 'yyyy-MM-dd')
  date = format.convertDateToString(new Date(), 'yyyy-MM-dd')
  var times = [preDate, date]
  return times
}
// 获取最新一周的日期
format.getDefaultWorkDatetime = function (defaultFormat = 'yyyy-MM-dd') {
  var date = new Date()
  var preDate = new Date()
  preDate.setDate(preDate.getDate() - 7)
  preDate = format.convertDateToString(preDate, defaultFormat)
  date = format.convertDateToString(date, defaultFormat)
  var times = [preDate, date]
  return times
}
// 获取当前月第一天和今天的日期
format.getMonthStartDatetime = function (defaultFormat = 'yyyy-MM-dd') {
  var date = new Date()
  var nowMonth = date.getMonth() // 当前月
  var nowYear = date.getFullYear() // 当前年
  var monthStartDate = new Date(nowYear, nowMonth, 1) // 本月开始的时间
  var timeStar = format.convertDateToString(monthStartDate, defaultFormat)
  date = format.convertDateToString(date, defaultFormat)
  var times = [timeStar, date]
  return times
}
/** *
 * 获取某个日期或当前日期所在季度的第一天
 * @param {*时间} time, 默认当前日期
 * */
format.getSeasonDayOne = function (time) {
  let date = new Date()
  const month = time ? (new Date(time).getMonth() || date.getMonth()) : date.getMonth()

  if (month < 3) {
    date.setMonth(0)
  } else if (month < 6) {
    date.setMonth(3)
  } else if (month < 9) {
    date.setMonth(6)
  } else if (month < 12) {
    date.setMonth(9)
  }
  date.setDate(1)
  return format.convertDateToString(date)
}
// 获取当前年第一天到今天
format.getYearStartDatetime = function (defaultFormat = 'yyyy-MM-dd') {
  var date = new Date()
  var nowYear = date.getFullYear() // 当前年
  var timeStar = nowYear + '-01-01'
  date = format.convertDateToString(date, defaultFormat)
  var times = [timeStar, date]
  return times
}

历时的复杂计算

/** *
 * 计算两个日期之间的历时
 * @param starttime
 * @param endtime
 * @returns {string}
 */
format.getDatetimeLs = function (starttime, endtime) {
  var dd = ''
  var hh = ''
  var mm = ''
  var ss = ''
  if (!endtime || endtime > new Date()) {
    return '未结束'
  }
  if (!starttime) {
    return ''
  }
  var mimis = (endtime - starttime) / 1000 // 秒
  if (mimis > 24 * 60 * 60) {
    // 大于1天,按照天计算
    dd = Math.floor(mimis / (24 * 60 * 60))
    mimis = mimis - dd * 24 * 60 * 60
    dd += '天'
  }
  if (mimis > 60 * 60) {
    // 大于1小时,按照小时计算
    hh = Math.floor(mimis / (60 * 60))
    mimis = mimis - hh * 60 * 60
    hh += '时'
  }
  if (mimis > 60) {
    // 大于1分钟,按照分钟计算
    mm = Math.floor(mimis / 60)
    mimis = mimis - mm * 60
    mm += '分'
  }
  if (mimis > 0) {
    // 剩余的秒
    ss = mimis + '秒'
  }
  return dd + hh + mm + ss
}

…其他有几个复杂的日期操作,请看另一篇文章:复杂时间处理方法

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值