日期方法 js

javaScript 日期

/** 对日期进行格式化
* 格式化为 YYYY-MM-dd 返回当前日期
*/
Date.prototype.format_cm_export_time = function () {
  var s = "";
  var mouth =
    this.getMonth() + 1 >= 10
      ? this.getMonth() + 1
      : "0" + (this.getMonth() + 1);
  var day = this.getDate() >= 10 ? this.getDate() : "0" + this.getDate();
  s += this.getFullYear() + "-";
  s += mouth + "-";
  s += day;
  return s;
};

/**
 * 默认向前推两天
 * pushDateYMD yyyy-MM-dd
 * @param num
 * @returns {*}
 */
Date.prototype.pushDateDayT = function (num) {
  if (num == undefined) num = -2;
  return new Date(
    this.getTime() + Number(num) * 24 * 60 * 60 * 1000
  ).format_cm_export_time();
};

/**
 * 默认向前推一周
 * pushDateYMD yyyy-MM-dd
 * @param num
 * @returns {*}
 */
Date.prototype.pushDateWeekT = function (num) {
  if (num == undefined) num = -1;
  //把当前日期 进行推改
  var timesp = new Date(this.getTime() + num * 7 * 24 * 60 * 60 * 1000);
  //对当前日期进行
  if (timesp.getDay() < 0) {
    num = -timesp.getDay() - 6;
  } else {
    num = -timesp.getDay() + 1;
  }
  return new Date(
    timesp.getTime() + num * 24 * 60 * 60 * 1000
  ).format_cm_export_time();
};

/** 进行特定的几个月前的日期
* 以每月 25 号为分界线
* 向前推一个月 or 两个月
*/
Date.prototype.getPreMontht = function () {
  var s = "";
  var year = this.getFullYear();
  var month =
    this.getMonth() + 1 >= 10
      ? this.getMonth() + 1
      : "0" + (this.getMonth() + 1);
  var day = this.getDate() >= 10 ? this.getDate() : "0" + this.getDate();
  var num = day < 25 ? 2 : 1;
  while (num != 0) {
    num--;
    month = parseInt(month) - 1;
    if (month == 0) {
      year = parseInt(year) - 1;
      month = 12;
    }
  }
  var day2 = new Date(year, month, 0);
  day2 = day2.getDate();
  if (day > day2) {
    day = day2;
  }
  month = month >= 10 ? month : "0" + month;
  s += year + "-";
  s += month + "";
  return s;
};
/**
 * 默认向前推一月
 * pushDateYMD yyyy-MM-dd
 * @param num
 * @returns {*}
 */
Date.prototype.pushDateMonthT = function (num) {
  if (num == undefined) num = -1;
  //把当前日期 进行推改
  var numy = 0;
  var time = this;
  var a = time.getDate();
  while (num != 0) {
    if (num > 0) {
      num--;
      var c =
        new Date(
          time.format_cm_export_time().substring(0, 4),
          time.format_cm_export_time().substring(5, 7),
          0
        ).getDate() - time.getDate();
      numy = numy + c + a;
    } else {
      num++;
      numy = numy - time.getDate();
    }
    time = new Date(this.getTime() + numy * 24 * 60 * 60 * 1000);
  }
  var b = time.getDate();
  if (a < b) {
    time = new Date(time.getTime() + (a - b) * 24 * 60 * 60 * 1000);
  }
  return time.format_cm_export_time();
};

/**
* 指定两个日期之间的list
* YYYYMMdd, YYYYMMdd
*/

Date.prototype.beginBetweenEnd = function (begin, end) {
  var db = new Date();
  db.setUTCFullYear(
    begin.substring(0, 4),
    begin.substring(4, 6) - 1,
    begin.substring(6, 8)
  );
  var de = new Date();
  de.setUTCFullYear(
    end.substring(0, 4),
    end.substring(4, 6) - 1,
    end.substring(6, 8)
  );
  var unixDb = db.getTime();
  var unixDe = de.getTime();
  var list = [];
  for (var k = unixDb; k <= unixDe;) {
    list.push(new Date(parseInt(k)).format_cm_export_time());
    k = k + 24 * 60 * 60 * 1000;
  }
  return list;
};

猜你喜欢

转载自blog.csdn.net/late_tender/article/details/90137467