miment.min.js
5.1 KB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.miment=e()}(this,function(){"use strict";function t(){var e=arguments[0];"string"==typeof e&&/^[\d-: ]*$/.test(e)&&(arguments[0]=e.replace(/-/g,"/"));var s=new(Function.prototype.bind.apply(Date,[Date].concat(Array.prototype.slice.call(arguments))));return Object.setPrototypeOf(s,t.prototype),Object.setPrototypeOf(t.prototype,Date.prototype),s}Object.setPrototypeOf=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t};var e=["日","一","二","三","四","五","六"];function s(t,e,s){switch(e){case"YY":case"YYYY":t=s?t:this.getFullYear()+t,this.setFullYear(t);break;case"MM":t=s?t-1:this.getMonth()+t,this.setMonth(t);break;case"DD":t=s?t:this.getDate()+t,this.setDate(t);break;case"hh":t=s?t:this.getHours()+t,this.setHours(t);break;case"mm":t=s?t:this.getMinutes()+t,this.setMinutes(t);break;case"ss":t=s?t:this.getSeconds()+t,this.setSeconds(t);break;case"SSS":t=s?t:this.getMilliseconds()+t,this.setMilliseconds(t);break;case"ww":case"WW":s?(this.setMonth(0),this.setDate(1),this.setDate(7*t),i.call(this)):this.setDate(this.getDate()+7*t)}}function i(){return this.setDate(this.getDate()-this.getDay()),this}return t.prototype.format=function(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD hh:mm:ss",i=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=void 0,n=void 0,a=void 0,o=void 0,h=void 0,c=void 0,u=void 0,l=void 0;if(i){var Y=void 0,f=void 0;this.__distance_begin__>this.__distance_end__?(Y=t(this.__distance_begin__),f=t(this.__distance_end__)):(Y=t(this.__distance_end__),f=t(this.__distance_begin__)),r=Y.getFullYear()-f.getFullYear(),n=Y.getMonth()-f.getMonth(),a=Y.getDate()-f.getDate(),o=Y.getHours()-f.getHours(),h=Y.getMinutes()-f.getMinutes(),c=Y.getSeconds()-f.getSeconds(),u=Math.abs(Y.getDay()-f.getDay()),(l=Math.abs(Y.getMilliseconds()-f.getMilliseconds()))<0&&(l+=1e3,c--),c<0&&(c+=60,h--),h<0&&(h+=60,o--),o<0&&(o+=24,a--),a<0&&(a+=f.daysInMonth(),n--),n<0&&(n+=12,r--)}else r=this.getFullYear(),n=this.getMonth()+1,a=this.getDate(),o=this.getHours(),h=this.getMinutes(),c=this.getSeconds(),u=this.getDay(),l=this.getMilliseconds();return s=s.replace("YYYY",r).replace("MM",String(n)[1]?n:"0"+n).replace("DD",String(a)[1]?a:"0"+a).replace("hh",String(o)[1]?o:"0"+o).replace("mm",String(h)[1]?h:"0"+h).replace("ss",String(c)[1]?c:"0"+c).replace("SSS",l).replace("ww",u),s=i?s.replace("WW",u):s.replace("WW",e[u])},t.prototype.stamp=function(){return this.valueOf()},t.prototype.json=function(){return{year:this.getFullYear(),month:this.getMonth()+1,date:this.getDate(),hour:this.getHours(),minute:this.getMinutes(),second:this.getSeconds(),day:this.getDay(),millisecond:this.getMilliseconds()}},t.prototype.get=function(t){var e=String(function(t){switch(t){case"YYYY":return this.getFullYear();case"MM":return this.getMonth()+1;case"DD":return this.getDate();case"hh":return this.getHours();case"mm":return this.getMinutes();case"ss":return this.getSeconds();case"ww":case"WW":return this.getDay();case"SSS":return this.getMilliseconds()}return"00"}.call(this,t));return e[1]||"SSS"===t||"millisecond"===t?e:"0"+e},t.prototype.diff=function(e,s){return s?t(e).valueOf()-t(s).valueOf():this.valueOf()-t(e).valueOf()},t.prototype.isBefore=function(e){return this.valueOf()<t(e).valueOf()},t.prototype.isAfter=function(e){return this.valueOf()>t(e).valueOf()},t.prototype.isBetween=function(e,s){e=t(e).valueOf(),s=t(s).valueOf();var i=this.valueOf();return e>i&&s<i||e<i&&s>i},t.prototype.daysInMonth=function(){return t(this.getFullYear(),this.getMonth()+1,0).getDate()},t.prototype.add=function(t,e){return t||(t=0),s.call(this,t,e),this},t.prototype.sub=function(t,e){return t||(t=0),s.call(this,-t,e),this},t.prototype.set=function(t,e){return t||(t=0),s.call(this,t,e,!0),this},t.prototype.distance=function(e,s){var i=void 0,r=void 0;s?(i=t(e).valueOf(),r=t(s).valueOf()):(i=this.valueOf(),r=t(e).valueOf());var n=t(i-r);return n.__distance_begin__=i,n.__distance_end__=r,n},t.prototype.startOf=function(){switch(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"DD"){case"mm":return t(this.format("YYYY-MM-DD hh:mm:00"));case"hh":return t(this.format("YYYY-MM-DD hh:00:00"));case"DD":return t(this.format("YYYY-MM-DD 00:00:00"));case"ww":case"WW":return t(this.firstDayOfWeek().format("YYYY-MM-DD 00:00:00"));case"MM":return t(this.format("YYYY-MM-01 00:00:00"));case"YY":case"YYYY":return t(this.format("YYYY-01-01 00:00:00"))}},t.prototype.endOf=function(){switch(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"DD"){case"mm":return t(this.format("YYYY-MM-DD hh:mm:59"));case"hh":return t(this.format("YYYY-MM-DD hh:59:59"));case"DD":return t(this.format("YYYY-MM-DD 23:59:59"));case"ww":case"WW":return t(this.firstDayOfWeek().add(6,"DD").format("YYYY-MM-DD 23:59:59"));case"MM":return t(this.lastDay().format("YYYY-MM-DD 23:59:59"));case"YY":case"YYYY":return t(this.set(12,"MM").lastDay().format("YYYY-MM-DD 23:59:59"))}},t.prototype.firstDay=function(){return t(this.getFullYear(),this.getMonth(),1)},t.prototype.lastDay=function(){return t(this.getFullYear(),this.getMonth()+1,0)},t.prototype.firstDayOfWeek=i,t});