javascript Date prototype 扩展

<script>
  Date.prototype.format = function (mask) {
    var d = this;
    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(/yy(?:yy)?|M{1,4}|d{1,4}|h(?:h)?|H(?:H)?|m(?:m)?|s(?:s)?|l|L|tt|TT|Z/g, function ($0) {
      switch ($0) {
        case 'd':
          return d.getDate();
        case 'dd':
          return zeroize(d.getDate());
        case 'ddd':
          return ['日', '一', '二', '三', '四', '五', '六'][d.getDay()];
        case 'dddd':
          return ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][d.getDay()];
        case 'M':
          return d.getMonth() + 1;
        case 'MM':
          return zeroize(d.getMonth() + 1);
        case 'MMM':
          return ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'][d.getMonth()];
        case 'MMMM':
          return ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'][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);
      }
    });
  };
  
  Date.prototype.prev = function () {
    var date = new Date(this);
    var n = Math.abs(arguments[0]);
    if (isNaN(n) || 1 > n) n = 1;
    date.setDate(date.getDate() - n);
    return date;
  };
  
  Date.prototype.next = function () {
    var date = new Date(this);
    var n = Math.abs(arguments[0]);
    if (isNaN(n) || 1 > n) n = 1;
    date.setDate(date.getDate() + n);
    return date;
  };
  
  Date.prototype.weekRangeToNow = function () {
    var day = this.getDay();
    if (0 == day) {
      return [this.prev(6), new Date(this)];
    } else {
      if (0 == (day - 1)) return [new Date(this), new Date(this)];
      return [this.prev(day - 1), new Date(this)];
    }
  };
  
  Date.prototype.weekRange = function () {
    var day = this.getDay();
    if (0 == day) {
      return [this.prev(6), new Date(this)];
    } else {
      var start = this.prev(day - 1);
      return [start, start.next(6)];
    }
  };
  
  Date.prototype.prevWeekRange = function () {
    var n = Math.abs(arguments[0]);
    var date = this.prev(7 * ((isNaN(n) || 1 > n) ? 1 : n));
    return date.weekRange();
  };
  
  Date.prototype.nextWeekRange = function () {
    var n = Math.abs(arguments[0]);
    var date = this.next(7 * ((isNaN(n) || 1 > n) ? 1 : n));
    return date.weekRange();
  };
  
  Date.prototype.monthRangeToNow = function () {
    return [new Date(this.getFullYear(), this.getMonth(), 1), new Date(this)];
  };
  
  Date.prototype.monthRange = function () {
    var date = new Date(this);
    date.setMonth(date.getMonth() + 1);
    date.setDate(0);
    return [new Date(this.getFullYear(), this.getMonth(), 1), date];
  };
  
  Date.prototype.prevMonthRange = function () {
    var date = new Date(this);
    var n = Math.abs(arguments[0]);
    if (isNaN(n) || 1 > n) n = 1;
    date.setMonth(date.getMonth() - (n - 1));
    date.setDate(0);
    return [new Date(date.getFullYear(), date.getMonth(), 1), date];
  };
  
  Date.prototype.nextMonthRange = function () {
    var date = new Date(this);
    var n = Math.abs(arguments[0]);
    if (isNaN(n) || 1 > n) n = 1;
    date.setMonth(date.getMonth() + (n + 1));
    date.setDate(0);
    return [new Date(date.getFullYear(), date.getMonth(), 1), date];
  };
  
  console.log("format => " + new Date().format("yyyy-MM-dd"));
  console.log("prev => " + new Date().prev().format("yyyy-MM-dd"));
  console.log("next => " + new Date().next().format("yyyy-MM-dd"));
  var range = new Date().weekRangeToNow();
  console.log("weekRangeToNow => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().weekRange();
  console.log("weekRange => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().prevWeekRange();
  console.log("prevWeekRange => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().nextWeekRange();
  console.log("nextWeekRange => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().monthRangeToNow();
  console.log("monthRangeToNow => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().monthRange();
  console.log("monthRange => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().prevMonthRange();
  console.log("prevMonthRange => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
  range = new Date().nextMonthRange();
  console.log("nextMonthRange => " + range[0].format("yyyy-MM-dd - ") + range[1].format("yyyy-MM-dd"));
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值