-
Notifications
You must be signed in to change notification settings - Fork 6
/
timeSolver.min.js
9 lines (9 loc) · 6.48 KB
/
timeSolver.min.js
1
2
3
4
5
6
7
8
9
/**
* timeSolver.min.js
*
* @description A small date time tool in JavaScript, see: https://github.com/sean1093/timeSolver/ for details
* @version v1.2.0
* @author Sean Chou
* @license [https://github.com/sean1093/timeSolver/blob/master/LICENSE] [Licensed under MIT]
*/
!function(){"use strict";var e={add:function(e,t,r){r=i(r),e=s(e),t=void 0===t?0:t;var o=null;switch(r){case 0:o=new Date(e.setMilliseconds(e.getMilliseconds()+t));break;case 1:o=new Date(e.setSeconds(e.getSeconds()+t));break;case 2:o=new Date(e.setMinutes(e.getMinutes()+t));break;case 3:o=new Date(e.setHours(e.getHours()+t));break;case 4:o=new Date(e.setDate(e.getDate()+t));break;case 5:o=new Date(e.setMonth(e.getMonth()+t));break;case 6:o=new Date(e.setFullYear(e.getFullYear()+t));break;default:console.error(a[0])}return o},subtract:function(e,t,r){r=i(r),e=s(e),t=void 0===t?0:t;var o=null;switch(r){case 0:o=new Date(e.setMilliseconds(e.getMilliseconds()-t));break;case 1:o=new Date(e.setSeconds(e.getSeconds()-t));break;case 2:o=new Date(e.setMinutes(e.getMinutes()-t));break;case 3:o=new Date(e.setHours(e.getHours()-t));break;case 4:o=new Date(e.setDate(e.getDate()-t));break;case 5:o=new Date(e.setMonth(e.getMonth()-t));break;case 6:o=new Date(e.setFullYear(e.getFullYear()-t));break;default:console.error(a[0])}return o},equal:function(e,t){return e=s(e),t=s(t),e.toString()===t.toString()},between:function(e,t,r){r=i(r),e=s(e);var o=(t=s(t)).getTime()-e.getTime(),n=1;switch(r){case 0:n=1;break;case 1:n=1e3;break;case 2:n=6e4;break;case 3:n=36e5;break;case 4:n=864e5;break;case 5:n=26298e5;break;case 6:n=315576e5;break;default:console.error(a[0])}return o/n},after:function(e,t,r){return!(this.between(e,t,r)>0)},afterToday:function(e){return this.after(e,new Date,"d")},before:function(e,t,r){return this.between(e,t,r)>0},beforeToday:function(e){return this.before(e,new Date,"d")},getString:function(e,t){t=void 0===t?"YYYYMMDD":t.toUpperCase();var r=(e=s(e)).getFullYear(),o=M(e.getMonth()+1),i=M(e.getDate()),Y=M(e.getHours()),l=M(e.getMinutes()),c=M(e.getSeconds()),u=M(e.getMilliseconds()),S=r.toString(),D=o.toString(),g=i.toString(),b=S+D+g,d=Y.toString()+":"+l.toString()+":"+c.toString(),h=d+"."+u.toString(),k={0:S,1:S+D,2:b,3:S+"/"+D+"/"+g,4:S+"-"+D+"-"+g,5:S+"."+D+"."+g,6:D+g+S,7:g+D+S,8:D+"/"+g+"/"+S,9:D+"-"+g+"-"+S,10:D+"."+g+"."+S,11:S+"/"+D+"/"+g+" "+d,12:S+"/"+D+"/"+g+" "+h,13:S+"-"+D+"-"+g+" "+d,14:S+"-"+D+"-"+g+" "+h,15:S+"."+D+"."+g+" "+d,16:S+"."+D+"."+g+" "+d,17:b+" "+d,18:b+" "+h,19:D+"/"+g+"/"+S+" "+d,20:D+"/"+g+"/"+S+" "+h,21:D+"-"+g+"-"+S+" "+d,22:D+"-"+g+"-"+S+" "+h,23:D+"."+g+"."+S+" "+d,24:D+"."+g+"."+S+" "+h,25:d,26:h};return k[n[t]]?k[n[t]]:a[0]},getAbbrWeek:function(e){return null!==s(e)?s(e).toString().substring(0,3):new Error(a[1])},getFullWeek:function(e){return r[s(e).getDay()]},getAbbrMonth:function(e){return null!==s(e)?s(e).toString().substring(3,7):new Error(a[1])},getFullMonth:function(e){return t[s(e).getMonth()]},isValid:function(e,t){var r=!0;if(void 0===t)"Invalid Date"==new Date(e)&&(r=!1);else switch(t=t.toUpperCase(),n[t]){case 3:o.a.test(e)||(r=!1);break;case 4:o.b.test(e)||(r=!1);break;case 5:o.c.test(e)||(r=!1);break;case 11:var s=e.split(" ");o.a.test(s[0])&&o.t.test(s[1])||(r=!1);break;case 13:s=e.split(" ");o.b.test(s[0])&&o.t.test(s[1])||(r=!1);break;case 15:s=e.split(" ");o.c.test(s[0])&&o.t.test(s[1])||(r=!1);break;case 25:s=e.split(" ");o.t.test(s[1])||(r=!1);break;default:console.error(a[0]),r=null}return r},getQuarterByMonth:function(e){return 1<=e&&e<=3?1:4<=e&&e<=6?2:7<=e&&e<=9?3:10<=e&&e<=12?4:null},getFirstMonthByQuarter:function(e){return 1==e?1:2==e?4:3==e?7:4==e?10:null},timeArray:[],timeLookMax:0,timeLookTotal:0,timeLookStart:function(){this.timeArray.length=0,this.timeLookMax=0,this.timeLookTotal=0,this.timeArray.push({label:"start",time:new Date,interval:0})},timeLook:function(e){var t=this.timeArray[this.timeArray.length-1],r=new Date,o=this.between(t.time,r,"S");this.timeLookTotal+=o,this.timeLookMax=o>this.timeLookMax?o:this.timeLookMax,this.timeArray.push({label:e,time:new Date,interval:o})},timeLookReport:function(){var t="color: #2962FF",r="color: #4CAF50",o=new Date;console.log("%c=================================",t),console.log("%c[timeSolver] Time Look Report","font-weight: bold; color: #3F51B5");for(var n=1;n<e.timeArray.length;n++){var a=e.timeArray[n].label,s=e.timeArray[n].interval,i=this.timeLookMax==s?"color: #ff0000":t;console.log("%c["+s+"s] "+Math.round(s/this.timeLookTotal*100)+"% "+a,i)}var M=new Date;console.log("%c[timeSolver] Spend "+this.between(o,M,"S")+"s to create this report",r),console.log("%c[timeSolver] For more information: https://github.com/sean1093/timeSolver#timelook",r),console.log("%c=================================",t)}},t=["January","February","March","April","May","June","July","August","September","October","November","December"],r=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],o={a:/^(\d{4})([/])((1|3|5|7|8|0[13578]|1[02])\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|(4|6|9|0[469]|11)\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0])|(02|2)\2([1-9]|0[1-9]|1[0-9]|2[0-8]))$/,b:/^(\d{4})([-])((1|3|5|7|8|0[13578]|1[02])\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|(4|6|9|0[469]|11)\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0])|(02|2)\2([1-9]|0[1-9]|1[0-9]|2[0-8]))$/,c:/^(\d{4})([.])((1|3|5|7|8|0[13578]|1[02])\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|(4|6|9|0[469]|11)\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0])|(02|2)\2([1-9]|0[1-9]|1[0-9]|2[0-8]))$/,t:/^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/},n={YYYY:0,YYYYMM:1,YYYYMMDD:2,"YYYY/MM/DD":3,"YYYY-MM-DD":4,"YYYY.MM.DD":5,MMDDYYYY:6,DDMMYYYY:7,"MM/DD/YYYY":8,"MM-DD-YYYY":9,"MM.DD.YYYY":10,"YYYY/MM/DD HH:MM:SS":11,"YYYY/MM/DD HH:MM:SS.SSS":12,"YYYY-MM-DD HH:MM:SS":13,"YYYY-MM-DD HH:MM:SS.SSS":14,"YYYY.MM.DD HH:MM:SS":15,"YYYY.MM.DD HH:MM:SS.SSS":16,"YYYYMMDD HH:MM:SS":17,"YYYYMMDD HH:MM:SS.SSS":18,"MM/DD/YYYY HH:MM:SS":19,"MM/DD/YYYY HH:MM:SS.SSS":20,"MM-DD-YYYY HH:MM:SS":21,"MM-DD-YYYY HH:MM:SS.SSS":22,"MM.DD.YYYY HH:MM:SS":23,"MM.DD.YYYY HH:MM:SS.SSS":24,"HH:MM:SS":25,"HH:MM:SS.SSS":26},a={0:"[timeSolver] Input Type Error",1:"[timeSolver] Invalid Date"},s=function(e){var t="object"!=typeof e?new Date(e):e;return"Invalid Date"==t?(console.error(a[1]),null):t},i=function(e){return"MILLISECOND"==(e=void 0===e?"MILLISECOND":e.toUpperCase())||"MILL"==e?e=0:"SECOND"==e||"S"==e?e=1:"MINUTE"==e||"MIN"==e?e=2:"HOUR"==e||"H"==e?e=3:"DAY"==e||"D"==e?e=4:"MONTH"==e||"M"==e?e=5:"YEAR"!=e&&"Y"!=e||(e=6),e},M=function(e){return e<10?"0"+e:e};"undefined"!=typeof module&&void 0!==module.exports?module.exports=e:window.timeSolver=e}();