欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js常用时间函数的封装

程序员文章站 2024-01-22 20:00:52
...

1、获取当前时间并整理成固定格式:兼容Mac

调用方法:let today = fomatTime(new Date(), 'yyyy-MM-dd')   // 2020-09-23

function fomatTime (now, mask) {
  let d
  if (typeof (now) === 'string') {
    d = new Date(Date.parse(now.replace(/-/g, '/')))
  } else {
    d = new Date(now)
  }
  var zeroize = function (value, length) {
    if (!length) length = 2
    value = String(value)
    for (var i = 0, zeros = ''; i < (length - value.length); i++) {
      zeros += '0'
    }
    return zeros + value
  }
  return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function ($0) {
    switch ($0) {
      case 'd': return d.getDate()
      case 'dd': return zeroize(d.getDate())
      case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][d.getDay()]
      case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()]
      case 'M': return d.getMonth() + 1
      case 'MM': return zeroize(d.getMonth() + 1)
      case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()]
      case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()]
      case 'yy': return String(d.getFullYear()).substr(2)
      case 'yyyy': return d.getFullYear()
      case 'h': return d.getHours() % 12 || 12
      case 'hh': return zeroize(d.getHours() % 12 || 12)
      case 'H': return d.getHours()
      case 'HH': return zeroize(d.getHours())
      case 'm': return d.getMinutes()
      case 'mm': return zeroize(d.getMinutes())
      case 's': return d.getSeconds()
      case 'ss': return zeroize(d.getSeconds())
      case 'l': return zeroize(d.getMilliseconds(), 3)
      case 'L': var m = d.getMilliseconds()
        if (m > 99) m = Math.round(m / 10)
        return zeroize(m)
      case 'tt': return d.getHours() < 12 ? 'am' : 'pm'
      case 'TT': return d.getHours() < 12 ? 'AM' : 'PM'
      case 'Z': return d.toUTCString().match(/[A-Z]+$/)
      // Return quoted strings with the surrounding quotes removed
      default: return $0.substr(1, $0.length - 2)
    }
  })
}

2. 获取前几天或者后几天的日期并结合上面的fomatTime整理格式:

// 调用
let yesterday = fomatTime(getDay(-1), 'yyyy-MM-dd')
let tomorrow = fomatTime(getDay(+1), 'yyyy-MM-dd')
function getDay(num) {
  var date1 = new Date()
  // 今天时间
  var time1 = date1.getFullYear() + '-' + (date1.getMonth() + 1) + '-' + date1.getDate()
  var date2 = new Date(date1)
  date2.setDate(date1.getDate() + num)
  // num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  var time2 = date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + date2.getDate();
  return time2
}

3.获取上个月,下个月:

// 调用
// 先获取当前月
let currentMonth = fomatTime(new Date(), 'yyyy-MM-dd')
let lastMonth = fomatTime(getPreMonth(currentMonth), 'yyyy-MM-dd')
// 获取上个月
function getPreMonth(date) {
  var arr = date.split('-');
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中月的天数
  var year2 = year;
  var month2 = parseInt(month) - 1;
  if (month2 == 0) {
    year2 = parseInt(year2) - 1;
    month2 = 12;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = '0' + month2;
  }
  var t2 = year2 + '-' + month2 + '-' + day2;
  return t2;
}

// 获取下个月
function getNextMonth(date) {
  var arr = date.split('-');
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中的月的天数
  var year2 = year;
  var month2 = parseInt(month) + 1;
  if (month2 == 13) {
    year2 = parseInt(year2) + 1;
    month2 = 1;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = '0' + month2;
  }

  var t2 = year2 + '-' + month2 + '-' + day2;
  return t2;
}

 

相关标签: js javascript