zdatejs
Version:
a plugin of date
2 lines (1 loc) • 4.83 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).zdatejs=t()}(this,function(){"use strict";function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function e(t){return function(e){return Object.prototype.toString.call(e)==="[object ".concat(t,"]")}}function f(e){return g[e]||e.toLowerCase()}function n(e){return e?new Date(e):new Date}function l(e,t,n){return e<100&&0<=e?new Date(e+400,t,n)-126227808e5:new Date(e,t,n).valueOf()}function i(e){if(e instanceof m)return e;var t={};return e&&(t.date=e),new m(t)}var t,s,a=e("Date"),u=e("String"),o=e("Undefined"),d=e("Number"),c=/^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/,v=/\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|SS{1,2}/g,h={year:"setFullYear",Y:"setFullYear",month:"setMonth",M:"setMonth",date:"setDate",day:"setDate",D:"setDate",hour:"setHours",H:"setHours",minute:"setMinutes",m:"setMinutes",second:"setSeconds",s:"setSeconds",millisecond:"setMilliseconds",ms:"setMilliseconds"},y=(t="日一二三四五六".split("")).length?t.reduce(function(e,t,n){return e[s&&t[s]?t[s]:n]=t,e},{}):(console.warning("传入数组为空"),null),g={y:"year",M:"month",D:"day",H:"hour",m:"minute",s:"second",ms:"millisecond"},m=function(){function t(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),this._date=this.parse(e.date)}return function(e,t,n){t&&r(e.prototype,t),n&&r(e,n)}(t,[{key:"set",value:function(e,t){["month","M"].includes(e)&&(t-=1),this._date[h[e]](t)}},{key:"parse",value:function(e){if(o(e))return n();if(a(e)||d(e))return n(e);if(u(e)&&c.test(e)){var t=e.match(c);if(t)return new Date(t[1],t[2]-1,t[3]||1,t[4]||0,t[5]||0,t[6]||0,t[7]||0)}return n()}},{key:"format",value:function(e){function t(e,t){return 0<t?String(e).padStart(t,"0"):String(e)}var n=0<arguments.length&&void 0!==e?e:"YYYY-MM-DD HH:mm:ss",r=this.year,i=this.month,s=this.date,a=this.hour,u=this.minute,o=this.second,d=this.millisecond,c=this.week,h={YY:t(r).slice(-2),YYYY:t(r),M:t(i+1),MM:t(i+1,2),D:t(s),DD:t(s,2),d:c,dd:y[c],H:t(a),HH:t(a,2),h:t(a%12),hh:t(a%12,2),m:t(u),mm:t(u,2),s:t(o),ss:t(o,2),S:t(d),SS:t(d,2)};return n.replace(v,function(e){return h[e]||e})}},{key:"startEnd",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:"start";if(!e||!this.isValid()||"millisecond"===e)return this;function r(e){return"end"===n?s.valueOf()+e-s.valueOf()%e-1:Math.floor(s.valueOf()/e)}var i,s=this._date,a=this.year,u=this.month,o=this.date,d=f(e),c="end"===n?1:0,h="end"===n?-1:0;switch(d){case"year":i=l(a+c,0,1)+h;break;case"month":i=l(a,u+c,1)+h;break;case"day":i=l(a,u,o+c)+h;break;case"hour":i=r(36e5);break;case"minute":i=r(6e4);break;case"second":i=r(1e3)}return this._date.setTime(i),this}},{key:"startOf",value:function(e){return this.startEnd(e,"start")}},{key:"endOf",value:function(e){return this.startEnd(e,"end")}},{key:"addSubtract",value:function(e,t,n){var r=f(t),i=0<n?e:-e;this._date[h[r]](this[r]+i)}},{key:"add",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:"s";return this.addSubtract(e,n,1),this}},{key:"subtract",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:"s";return this.addSubtract(e,n,-1),this}},{key:"getTime",value:function(e){return n(this.parse(e)).getTime()}},{key:"isLeapYear",value:function(e){var t=this.parse(e).getFullYear();return t%4==0&&t%100!=0||t%400==0}},{key:"isBefore",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:"s";return this.endOf(n)>this.parse(e)}},{key:"isAfter",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:"s";return this.startOf(n)>this.parse(e)}},{key:"isSame",value:function(e,t){var n=1<arguments.length&&void 0!==t?t:"s";return this.startOf(n)===i(e).startOf(n)}},{key:"isBetween",value:function(e,t,n){var r=2<arguments.length&&void 0!==n?n:"s",i=this.startOf(r),s=this.endOf(r),a=this.parse(e),u=this.parse(t);return a<=i&&s<=u||u<=i&&s<=a}},{key:"isValid",value:function(){return!("Invalid Date"===this._date.toString())}},{key:"valueOf",value:function(){return this.getTime()}},{key:"year",get:function(){return this._date.getFullYear()}},{key:"month",get:function(){return this._date.getMonth()}},{key:"date",get:function(){return this._date.getDate()}},{key:"day",get:function(){return this._date.getDate()}},{key:"hour",get:function(){return this._date.getHours()}},{key:"minute",get:function(){return this._date.getMinutes()}},{key:"second",get:function(){return this._date.getSeconds()}},{key:"millisecond",get:function(){return this._date.getMilliseconds()}},{key:"week",get:function(){return this._date.getDay()}}]),t}();return i});