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>

發佈了6 篇原創文章 · 獲贊 0 · 訪問量 1507
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章