javascript時間日期格式化和獲取封裝類

antzone發表於2017-04-13

分享一段程式碼例項,它在Date原型新增了比較常用的一些對時間日期的操作的方法。

比如獲判斷是否為閏年,獲取月份的天數,或者格式化時間日期等。

程式碼例項如下:

[JavaScript] 純文字檢視 複製程式碼
/**
 * 時間工具類
 */
/**
 * 獲取指定月份的最大天數
 * @param month 指定月份
 * @returns {number} 天數
 */
Date.prototype.getMonthDays = function(month) {
  var tempDate = this;
  return new Date(tempDate.getFullYear(), month, 0).getDate();
};
 
/**
 * 獲取當月的最大天數
 * @returns {number} 天數
 */
Date.prototype.getSameMonthDays = function() {
  var tempDate = this;
  return new Date(tempDate.getFullYear(), tempDate.getMonth() + 1, 0).getDate();
};
 
/**
 * 判斷當前年份是否為閏年
 * @returns {boolean} 布林值
 */
Date.prototype.isLeapYear = function() {
  return (0 == this.getYear() % 4 && ((this.getYear() % 100 != 0) || (this.getYear() % 400 == 0)));
};
 
/**
 * 格式化日期格式
 * YYYY/yyyy/YY/yy 表示年份
 * MM/M 月份
 * W/w 星期
 * dd/DD/d/D 日期
 * hh/HH/h/H 時間
 * mm/m 分鐘
 * ss/SS/s/S 秒
 * @param formatStr 格式化規則
 * @returns {string} 格式化後的字串
 */
Date.prototype.format = function(formatStr) {
  var str = formatStr;
  var Week = ['日', '一', '二', '三', '四', '五', '六'];
 
  str = str.replace(/yyyy|YYYY/, this.getFullYear());
  str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
 
  str = str.replace(/MM/, this.getMonth() > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
  str = str.replace(/M/g, (this.getMonth() + 1));
 
  str = str.replace(/w|W/g, Week[this.getDay()]);
 
  str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
  str = str.replace(/d|D/g, this.getDate());
 
  str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
  str = str.replace(/h|H/g, this.getHours());
  str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
  str = str.replace(/m/g, this.getMinutes());
 
  str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
  str = str.replace(/s|S/g, this.getSeconds());
 
  return str;
};
 
/**
 * 計算兩個執行日期的差值
 * @param DateOne 開始日期
 * @param DateTwo 結束日期
 * @returns {number} 差值
 */
function daysBetween(DateOne, DateTwo) {
  var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'));
  var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1);
  var OneYear = DateOne.substring(0, DateOne.indexOf('-'));
 
  var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'));
  var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1);
  var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));
 
  var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000);
 
  return Math.abs(cha);
};
 
/**
 * 把當前日期切割成陣列
 * @returns {Array} 返回切割後的日期陣列
 */
Date.prototype.toArray = function() {
  var myDate = this;
  var myArray = Array();
 
  myArray[0] = myDate.getFullYear();
  myArray[1] = myDate.getMonth() + 1;
  myArray[2] = myDate.getDate();
  myArray[3] = myDate.getHours();
  myArray[4] = myDate.getMinutes();
  myArray[5] = myDate.getSeconds();
 
  return myArray;
};
 
/**
 * 計算本月每天對應的星期
 * @returns {Array} 星期陣列
 */
Date.prototype.getWeekList = function() {
  var date = new Date();
  date.setDate(1);
  var week = date.getDay();
  var weeks;
  var constDayOfWeek = [];
  for (var j = week; j <= date.getMonthDays(date.getMonth() + 1) - 1 + week; j++) {
    switch (j % 7) {
      case 1:
        weeks = 1;
        break;
      case 2:
        weeks = 2;
        break;
      case 3:
        weeks = 3;
        break;
      case 4:
        weeks = 4;
        break;
      case 5:
        weeks = 5;
        break;
      case 6:
        weeks = 6;
        break;
      case 0:
        weeks = 0;
        break;
    }
    constDayOfWeek.push(weeks);
  }
  return constDayOfWeek;
};
 
/**
 * 字串轉為日期型別
 * @param DateStr 日期字串
 * @returns {Date} 對應日期
 * @constructor
 */
function stringToDate(DateStr) {
  var converted = Date.parse(DateStr);
  var myDate = new Date(converted);
 
  if (isNaN(myDate)) {
    var arys = DateStr.split('-');
    myDate = new Date(arys[0], --arys[1], arys[2]);
  }
 
  return myDate;
};
 
/**
 * 獲取指定時間的星期數
 * @param tempDateStr 指定的時間
 * @returns {Array} 星期數
 */
function getSameWeekList(tempDateStr) {
  var tempDate = stringToDate(tempDateStr);
 
  tempDate.setDate(1);
  var week = tempDate.getDay();
  var weeks;
  var constDayOfWeek = [];
  for (var j = week; j <= tempDate.getMonthDays(tempDate.getMonth() + 1) - 1 + week; j++) {
    switch (j % 7) {
      case 1:
        weeks = 1;
        break;
      case 2:
        weeks = 2;
        break;
      case 3:
        weeks = 3;
        break;
      case 4:
        weeks = 4;
        break;
      case 5:
        weeks = 5;
        break;
      case 6:
        weeks = 6;
        break;
      case 0:
        weeks = 0;
        break;
    }
    constDayOfWeek.push(weeks);
  }
 
  return constDayOfWeek;
};

相關文章