JavaScript之Date日期对象扩展

 各种时间加减 收藏起来以备后用

   //名称:日期加法函数
   //参数:part(year、month、day、hour、minute、second、millisecond)
   //返回:Date对象
   Date.prototype.add = function(part, value) {
       if (!value || isNaN(value)) value = 0;
       switch (part) {
           case "year":
               this.setFullYear(this.getFullYear() + value);
               break;
           case "month":
               this.setMonth(this.getMonth() + value);
               break;
           case "day":
               this.setDate(this.getDate() + value);
               break;
           case "hour":
               this.setHours(this.getHours() + value);
               break;
           case "minute":
               this.setMinutes(this.getMinutes() + value);
               break;
           case "second":
               this.setSeconds(this.getSeconds() + value);
               break;
           case "millisecond":
               this.setMilliseconds(this.getMilliseconds() + value);
               break;
           default:
       }
       return this;
   };

   Date.prototype.addYears = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setFullYear(this.getFullYear() + value);
       return this;
   };

   Date.prototype.addMonths = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setMonth(this.getMonth() + value);
       return this;
   };

   Date.prototype.addDays = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setDate(this.getDate() + value);
       return this;
   };

   Date.prototype.addHours = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setHours(this.getHours() + value);
       return this;
   };

   Date.prototype.addMinutes = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setMinutes(this.getMinutes() + value);
       return this;
   };

   Date.prototype.addSeconds = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setSeconds(this.getSeconds() + value);
       return this;
   };

   Date.prototype.addMilliseconds = function(value) {
       if (!value || isNaN(value)) value = 0;
       this.setMilliseconds(this.getMilliseconds() + value);
       return this;
   };

   //名称:日期加法函数
   //参数:time(日期字符串,示例:12:00:00)
   //返回:Date对象
   Date.prototype.addTime = function(time) {
       var timeRegex = /^([0-1]?d|2[0-3])(:[0-5]?d){1,2}$/g;
       if (timeRegex.test(time)) {
           var value = Date.parse("1970/1/1 " + time) - Date.parse("1970/1/1");
           this.setMilliseconds(this.getMilliseconds() + value);
       }
       return this;
   };

   //名称:日期格式化函数
   //参数:format(示例:yyyy-MM-dd hh:mm:ss)、zeroize(是否补零)
   //返回:日期字符串
   Date.prototype.toCustomString = function(format, zeroize) {
       if (!zeroize) zeroize = false;
       var dy = this.getFullYear();
       var dM = this.getMonth() + 1;
       var dd = this.getDate();
       var dh = this.getHours();
       var dm = this.getMinutes();
       var ds = this.getSeconds();
       var dS = this.getMilliseconds();
       var orm = {
           "y+": dy.toString(),
           "M+": !zeroize ? dM.toString() : dM < 10 ? ‘0‘ + dM : dM.toString(),
           "d+": !zeroize ? dd.toString() : dd < 10 ? ‘0‘ + dd : dd.toString(),
           "h+": !zeroize ? dh.toString() : dh < 10 ? ‘0‘ + dh : dh.toString(),
           "m+": !zeroize ? dm.toString() : dm < 10 ? ‘0‘ + dm : dm.toString(),
           "s+": !zeroize ? ds.toString() : ds < 10 ? ‘0‘ + ds : ds.toString(),
           "S": dS.toString()
       };
       for (var i in orm) {
           var patt = new RegExp(i);
           if (patt.test(format)) {
               var item = orm[i];
               var ms = format.match(patt);
               var result = ms[0];
               if (i === "S") {
                   format = format.replace(result, item);
               } else {
                   format = format.replace(result, item.substr(item.length - result.length));
               }
           }
       }
       return format;
   };

如果这篇文章对您有帮助,您可以打赏我

技术交流QQ群:15129679

原文地址:https://www.cnblogs.com/yeminglong/p/9857637.html