#代码输出-js中日期的扩展方法,如添加秒分时天,获取本月上月等
发布于 3 年前 作者 yangjia 491 次浏览 来自 分享

一、Date扩展方法源码

/////////////////////////////////////////////////////////////////
//author:peng   
//wx: aniupeng
/////////////////////////////////////////////////////////////////
/**
 * 为指定时间加秒
 * [@param](/user/param) number 几秒数 
 */
Date.prototype.addSeconds = function (number) {
  return new Date(Date.parse(this) + (1000 * number));
}
/**
 * 为指定时间加分钟
 * [@param](/user/param) number 几分钟 
 */
Date.prototype.addMinutes = function (number) {
  return new Date(Date.parse(this) + (60000 * number));
}
/**
 * 为指定时间加小时
 * [@param](/user/param) number 几小时 
 */
Date.prototype.addHours = function (number) {
  return new Date(Date.parse(this) + (3600000 * number));
}
/**
 * 为指定时间加天数
 * [@param](/user/param) number 几天
 */
Date.prototype.addDays = function (number) {
  return new Date(Date.parse(this) + (86400000 * number));
}
/**
 * 为指定时间加星期
 * [@param](/user/param) number 几星期
 */
Date.prototype.addWeeks = function (number) {
  return new Date(Date.parse(this) + ((86400000 * 7) * number));
}
/**
 * 为指定时间加月份
 * [@param](/user/param) number 几个月
 */
Date.prototype.addMonths = function (number) {
  return new Date(this.getFullYear(), (this.getMonth()) + number, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
}
/**
 * 为指定时间加季度
 * [@param](/user/param) number 几季度
 */
Date.prototype.addQuarters = function (number) {
  return new Date(this.getFullYear(), (this.getMonth()) + number * 3, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
}
/**
 * 为指定时间加年份
 * [@param](/user/param) number 几年
 */
Date.prototype.addYears = function (number) {
  return new Date((this.getFullYear() + number), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
}

/**
 * 日期格式化
 * 对Date的扩展,将 Date 转化为指定格式的String   
 * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,   
 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
 * [@param](/user/param)  fmt 格式化字符串 举例:(new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2006-07-02 08:09:04.423   (new Date()).Format("yyyy-M-d H:m:s.S")      ==> 2006-7-2 8:9:4.18 
 */
Date.prototype.format = function (fmt) { //author: meizz   
  var o = {
    "M+": this.getMonth() + 1, //月份   
    "d+": this.getDate(), //日   
    "H+": this.getHours(), //小时   
    "m+": this.getMinutes(), //分   
    "s+": this.getSeconds(), //秒   
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
    "S": this.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

/**
 * 获取昨天的区间 如:2021-09-27 0:00:00 ~ 2021-09-27 23:59:59
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.lastDay = function (startFmt, endFmt) {
  var d = this.addDays(-1);
  if (!startFmt) startFmt = "yyyy-MM-dd 0:00:00";
  if (!endFmt) endFmt = "yyyy-MM-dd 23:59:59";
  return _startAndEnd(d, d, startFmt, endFmt);
}
/**
 * 获取今天的区间 如:2021-09-28 0:00:00 ~ 2021-09-28 23:59:59
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.thisDay = function (startFmt, endFmt) {
  var d = this;
  if (!startFmt) startFmt = "yyyy-MM-dd 0:00:00";
  if (!endFmt) endFmt = "yyyy-MM-dd 23:59:59";
  return _startAndEnd(d, d, startFmt, endFmt);

}
/**
 * 获取明天的区间 如:2021-09-29 0:00:00 ~ 2021-09-29 23:59:59
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.nextDay = function (startFmt, endFmt) {
  var d = this.addDays(1);
  if (!startFmt) startFmt = "yyyy-MM-dd 0:00:00";
  if (!endFmt) endFmt = "yyyy-MM-dd 23:59:59";
  return _startAndEnd(d, d, startFmt, endFmt);
}
/**
 * 获取上周的区间 如:2021-09-20 ~ 2021-09-26
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.lastWeek = function (startFmt, endFmt) {
  var now = this;
  var day = now.getDay(); // 一周中的第几天0~6
  if(day==0) day = 7; // 国外是星期天作为一个星期的第一天,代码规则是国外定的,所以0是星期天
  if (!startFmt) startFmt = "yyyy-MM-dd";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now.addDays(-((day-1) + 7)), now.addDays(-day ), startFmt, endFmt);
}
/**
 * 获取本周的区间 如:2021-09-27 ~ 2021-10-03
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.thisWeek = function (startFmt, endFmt) {
  var now = this;
  var day = now.getDay(); // 一周中的第几天0~6
  if(day==0) day = 7; // 国外是星期天作为一个星期的第一天,代码规则是国外定的,所以0是星期天
  if (!startFmt) startFmt = "yyyy-MM-dd";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now.addDays(-(day-1)), now.addDays(7 - day), startFmt, endFmt);    
}
/**
 * 获取下周的区间 如:2021-10-04 ~ 2021-10-10
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.nextWeek = function (startFmt, endFmt) {
  var now = this;
  var day = now.getDay(); // 一周中的第几天0~6
  if(day==0) day = 7; // 国外是星期天作为一个星期的第一天,代码规则是国外定的,所以0是星期天
  if (!startFmt) startFmt = "yyyy-MM-dd";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now.addDays(7 - (day-1)), now.addDays(7 - day + 7), startFmt, endFmt);
}
/**
 * 获取上月的区间 如:2021-08-01 ~ 2021-08-31
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.lastMonth = function (startFmt, endFmt) {
  var now = this;
  if (!startFmt) startFmt = "yyyy-MM-01";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now.addMonths(-1), now.addDays(-now.getDate()), startFmt, endFmt);
}
/**
 * 获取本月的区间 如:2021-09-01 ~ 2021-09-30
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.thisMonth = function (startFmt, endFmt) {
  var now = this;
  if (!startFmt) startFmt = "yyyy-MM-01";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now, now.addMonths(1).addDays(-now.getDate()), startFmt, endFmt);
}
/**
 * 获取下个月的区间 如:2021-10-01 ~ 2021-10-31
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.nextMonth = function (startFmt, endFmt) {
  var now = this;
  if (!startFmt) startFmt = "yyyy-MM-01";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now.addMonths(1), now.addMonths(2).addDays(-now.getDate()), startFmt, endFmt);
}
/**
 * 获取上个季度的区间 如:2021-04-01 ~ 2021-06-30
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.lastQuarter = function (startFmt, endFmt) {
  var now = this;
  now = now.addMonths(-3 - (now.getMonth()) % 3);
  if (!startFmt) startFmt = "yyyy-MM-01";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now, now.addMonths(3).addDays(-now.getDate()), startFmt, endFmt);
}
/**
 * 获取本季度的区间 如:2021-07-01 ~ 2021-09-30
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.thisQuarter = function (startFmt, endFmt) {
  var now = this;
  now = now.addMonths(-(now.getMonth()) % 3);
  if (!startFmt) startFmt = "yyyy-MM-01";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now, now.addMonths(3).addDays(-now.getDate()), startFmt, endFmt);
}
/**
 * 获取下个季度的区间 如:2021-10-01 ~ 2021-12-31
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.nextQuarter = function (startFmt, endFmt) {
  var now = this;
  now = now.addMonths(3 - (now.getMonth()) % 3);
  if (!startFmt) startFmt = "yyyy-MM-01";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return _startAndEnd(now, now.addMonths(3).addDays(-now.getDate()), startFmt, endFmt);
}
/**
 * 获取上一年的区间 如:2020-01-01 ~ 2020-12-31
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.lastYear = function (startFmt, endFmt) {
  var now = new Date(this.getFullYear() - 1, 0, 1);
  return _startAndEnd(now, now.addYears(1).addDays(-1), startFmt, endFmt);
}
/**
 * 获取本年的区间 如:2021-01-01 ~ 2021-12-31
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.thisYear = function (startFmt, endFmt) {
  var now = new Date(this.getFullYear(), 0, 1);
  return _startAndEnd(now, now.addYears(1).addDays(-1), startFmt, endFmt);
}
/**
 * 获取下一年的区间 如:2022-01-01 ~ 2022-12-31
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
Date.prototype.nextYear = function (startFmt, endFmt) {
  var now = new Date(this.getFullYear() + 1, 0, 1);
  return _startAndEnd(now, now.addYears(1).addDays(-1), startFmt, endFmt);
}
/**
 * 根据时间与格式化字符串返回时间区间
 * [@param](/user/param) start 开始时间
 * [@param](/user/param) end 结束时间
 * [@param](/user/param) startFmt 开始时间格式化字符串
 * [@param](/user/param) endFmt 结束时间格式化字符串
 */
function _startAndEnd(start, end, startFmt, endFmt) {
  if (!end) end = start;
  if (!startFmt) startFmt = "yyyy-MM-dd";
  if (!endFmt) endFmt = "yyyy-MM-dd";
  return {
    "start": start.format(startFmt),
    "end": end.format(endFmt)
  };
}

二、扩展方法调用示例

// 示例:这里可以注释掉
var sample = {
  "1.当前时间加10秒": new Date().addSeconds(10).format("yyyy-MM-dd HH:mm:ss"),
  "2.当前时间加10分钟": new Date().addMinutes(10).format("yyyy-MM-dd HH:mm:ss"),
  "3.当前时间加10天": new Date().addDays(10).format("yyyy-MM-dd HH:mm:ss"),
  "4.当前时间加10星期": new Date().addWeeks(10).format("yyyy-MM-dd HH:mm:ss"),
  "5.当前时间加10个月": new Date().addMonths(10).format("yyyy-MM-dd HH:mm:ss"),
  "6.当前时间加10季度": new Date().addQuarters(10).format("yyyy-MM-dd HH:mm:ss"),
  "7.当前时间加10年": new Date().addYears(10).format("yyyy-MM-dd HH:mm:ss"),
  "8.昨天时间区间": new Date().lastDay(),
  "9.当天时间区间": new Date().thisDay(),
  "10.明天时间区间": new Date().nextDay(),
  "11.上周时间区间": new Date().lastWeek(),
  "12.本周时间区间": new Date().thisWeek(),
  "13.下周时间区间": new Date().nextWeek(),
  "14.上月时间区间": new Date().lastMonth(),
  "15.本月时间区间": new Date().thisMonth(),
  "16.下月时间区间": new Date().nextMonth(),
  "17.上季度时间区间": new Date().lastQuarter(),
  "18.本季度时间区间": new Date().thisQuarter(),
  "19.下季度时间区间": new Date().nextQuarter(),
  "20.上年时间区间": new Date().lastYear(),
  "21.本年时间区间": new Date().thisYear(),
  "22.下年时间区间": new Date().nextYear(),
  "23.格式化示例1": new Date().format("yyyy年MM月dd日"),
  "24.格式化示例2": new Date().format("yyyy/MM/dd"),
  "25.格式化示例3": new Date().format("yyyy-MM-dd")
};
console.log(sample)

示例输出结果:

1.当前时间加10秒: "2021-09-28 12:55:33"
2.当前时间加10分钟: "2021-09-28 13:05:23"
3.当前时间加10天: "2021-10-08 12:55:23"
4.当前时间加10星期: "2021-12-07 12:55:23"
5.当前时间加10个月: "2022-07-28 12:55:23"
6.当前时间加10季度: "2024-03-28 12:55:23"
7.当前时间加10年: "2031-09-28 12:55:23"
8.昨天时间区间: {start: “2021-09-27 0:00:00”, end: “2021-09-27 23:59:59”}
9.当天时间区间: {start: “2021-09-28 0:00:00”, end: “2021-09-28 23:59:59”}
10.明天时间区间: {start: “2021-09-29 0:00:00”, end: “2021-09-29 23:59:59”}
11.上周时间区间: {start: “2021-09-20”, end: “2021-09-26”}
12.本周时间区间: {start: “2021-09-27”, end: “2021-10-03”}
13.下周时间区间: {start: “2021-10-04”, end: “2021-10-10”}
14.上月时间区间: {start: “2021-08-01”, end: “2021-08-31”}
15.本月时间区间: {start: “2021-09-01”, end: “2021-09-30”}
16.下月时间区间: {start: “2021-10-01”, end: “2021-10-31”}
17.上季度时间区间: {start: “2021-04-01”, end: “2021-06-30”}
18.本季度时间区间: {start: “2021-07-01”, end: “2021-09-30”}
19.下季度时间区间: {start: “2021-10-01”, end: “2021-12-31”}
20.上年时间区间: {start: “2020-01-01”, end: “2020-12-31”}
21.本年时间区间: {start: “2021-01-01”, end: “2021-12-31”}
22.下年时间区间: {start: “2022-01-01”, end: “2022-12-31”}
23.格式化示例1: "2021年09月28日"
24.格式化示例2: "2021/09/28"
25.格式化示例3: “2021-09-28”

三、小程序页面js中调用

方式1.直接将扩展源码,拷贝到Page()前,然后在相关方法中调用
方式2.将源码保存到一个新建的js文件中,使用require引入调用

声明

调用

回到顶部