UNPKG

fin-date-util

Version:

Date for finance Nodejs

3 lines 12.6 kB
(function(){var g=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];var e=["January","February","March","April","May","June","July","August","September","October","November","December"];var p=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];var k=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];var r={"su":0,"sun":0,"sunday":0,"mo":1,"mon":1,"monday":1,"tu":2,"tue":2,"tuesday":2,"we":3,"wed":3,"wednesday":3,"th":4,"thu":4,"thursday":4,"fr":5,"fri":5,"friday":5,"sa":6,"sat":6,"saturday":6};var l=e.concat(g);var c=["su","sun","sunday","mo","mon","monday","tu","tue","tuesday","we","wed","wednesday","th","thu","thursday","fr","fri","friday","sa","sat","saturday"];var h={"jan":0,"january":0,"feb":1,"february":1,"mar":2,"march":2,"apr":3,"april":3,"may":4,"jun":5,"june":5,"jul":6,"july":6,"aug":7,"august":7,"sep":8,"september":8,"oct":9,"october":9,"nov":10,"november":10,"dec":11,"december":11};var b=[31,28,31,30,31,30,31,31,30,31,30,31];function o(t,s){t=String(t);while(t.length<s){t="0"+t}return t}var n=function(s){if(s.match(/^(\d+)$/)){return true}return false};var j=function(y,v,w,u){for(var s=u;s>=w;s--){var t=y.substring(v,v+s);if(t.length<w){return null}if(n(t)){return t}}return null};var i=Date.parse;var f=function(P,I){P=P+"";I=I+"";var N=0;var A=0;var K="";var v="";var O="";var B,z;var t=new Date();var C=t.getYear();var M=t.getMonth()+1;var L=1;var u=0;var J=0;var F=0;var D="";while(A<I.length){K=I.charAt(A);v="";while((I.charAt(A)===K)&&(A<I.length)){v+=I.charAt(A++)}if(v==="yyyy"||v==="yy"||v==="y"){if(v==="yyyy"){B=4;z=4}if(v==="yy"){B=2;z=2}if(v==="y"){B=2;z=4}C=j(P,N,B,z);if(C===null){return NaN}N+=C.length;if(C.length===2){if(C>70){C=1900+(C-0)}else{C=2000+(C-0)}}}else{if(v==="MMM"||v==="NNN"){M=0;for(var G=0;G<l.length;G++){var w=l[G];if(P.substring(N,N+w.length).toLowerCase()===w.toLowerCase()){if(v==="MMM"||(v==="NNN"&&G>11)){M=G+1;if(M>12){M-=12}N+=w.length;break}}}if((M<1)||(M>12)){return NaN}}else{if(v==="EE"||v==="E"){for(var E=0;E<c.length;E++){var H=c[E];if(P.substring(N,N+H.length).toLowerCase()===H.toLowerCase()){N+=H.length;break}}}else{if(v==="MM"||v==="M"){M=j(P,N,v.length,2);if(M===null||(M<1)||(M>12)){return NaN}N+=M.length}else{if(v==="dd"||v==="d"){L=j(P,N,v.length,2);if(L===null||(L<1)||(L>31)){return NaN}N+=L.length}else{if(v==="hh"||v==="h"){u=j(P,N,v.length,2);if(u===null||(u<1)||(u>12)){return NaN}N+=u.length}else{if(v==="HH"||v==="H"){u=j(P,N,v.length,2);if(u===null||(u<0)||(u>23)){return NaN}N+=u.length}else{if(v==="KK"||v==="K"){u=j(P,N,v.length,2);if(u===null||(u<0)||(u>11)){return NaN}N+=u.length}else{if(v==="kk"||v==="k"){u=j(P,N,v.length,2);if(u===null||(u<1)||(u>24)){return NaN}N+=u.length;u--}else{if(v==="mm"||v==="m"){J=j(P,N,v.length,2);if(J===null||(J<0)||(J>59)){return NaN}N+=J.length}else{if(v==="ss"||v==="s"){F=j(P,N,v.length,2);if(F===null||(F<0)||(F>59)){return NaN}N+=F.length}else{if(v==="a"){if(P.substring(N,N+2).toLowerCase()==="am"){D="AM"}else{if(P.substring(N,N+2).toLowerCase()==="pm"){D="PM"}else{return NaN}}N+=2}else{if(P.substring(N,N+v.length)!==v){return NaN}else{N+=v.length}}}}}}}}}}}}}}if(N!==P.length){return NaN}if(M===2){if(((C%4===0)&&(C%100!==0))||(C%400===0)){if(L>29){return NaN}}else{if(L>28){return NaN}}}if((M===4)||(M===6)||(M===9)||(M===11)){if(L>30){return NaN}}if(u<12&&D==="PM"){u=u-0+12}else{if(u>11&&D==="AM"){u-=12}}var s=new Date(C,M-1,L,u,J,F);return s.getTime()};Date.parse=function(t,w){if(w){return f(t,w)}var v=i(t),u=0,s;if(isNaN(v)&&(s=/^(\d{4}|[+\-]\d{6})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?))?/.exec(t))){if(s[8]!=="Z"){u=+s[10]*60+(+s[11]);if(s[9]==="+"){u=0-u}}s[7]=s[7]||"000";v=Date.UTC(+s[1],+s[2]-1,+s[3],+s[4],+s[5]+u,+s[6],+s[7].substr(0,3))}return v};function q(s,t){if(Date.prototype[s]===undefined){Date.prototype[s]=t}}Date.today=function(){return new Date().clearTime()};Date.UTCtoday=function(){return new Date().clearUTCTime()};Date.tomorrow=function(){return Date.today().add({days:1})};Date.UTCtomorrow=function(){return Date.UTCtoday().add({days:1})};Date.yesterday=function(){return Date.today().add({days:-1})};Date.UTCyesterday=function(){return Date.UTCtoday().add({days:-1})};Date.validateDay=function(s,u,v){var t=new Date(u,v,s);return(t.getFullYear()===u&&t.getMonth()===v&&t.getDate()===s)};Date.validateYear=function(s){return(s>=0&&s<=9999)};Date.validateSecond=function(s){return(s>=0&&s<60)};Date.validateMonth=function(s){return(s>=0&&s<12)};Date.validateMinute=function(s){return(s>=0&&s<60)};Date.validateMillisecond=function(s){return(s>=0&&s<1000)};Date.validateHour=function(s){return(s>=0&&s<24)};Date.compare=function(t,s){if(t.valueOf()<s.valueOf()){return -1}else{if(t.valueOf()>s.valueOf()){return 1}}return 0};Date.equals=function(t,s){return t.valueOf()===s.valueOf()};Date.equalsDay=function(t,s){return t.toYMD()===s.toYMD()};Date.getDayNumberFromName=function(s){return r[s.toLowerCase()]};Date.getMonthNumberFromName=function(s){return h[s.toLowerCase()] };Date.getMonthNameFromNumber=function(s){return e[s]};Date.getMonthAbbrFromNumber=function(s){return g[s]};Date.isLeapYear=function(s){return(new Date(s,1,29).getDate()===29)};Date.getDaysInMonth=function(s,t){if(t===1){return Date.isLeapYear(s)?29:28}return b[t]};q("getMonthAbbr",function(){return g[this.getMonth()]});q("getMonthName",function(){return e[this.getMonth()]});q("getLastMonthName",function(){var s=this.getMonth();s=(s==0?11:s-1);return e[s]});q("getUTCOffset",function(){var s=o(Math.abs(this.getTimezoneOffset()/0.6),4);if(this.getTimezoneOffset()>0){s="-"+s}return s});q("toCLFString",function(){return o(this.getDate(),2)+"/"+this.getMonthAbbr()+"/"+this.getFullYear()+":"+o(this.getHours(),2)+":"+o(this.getMinutes(),2)+":"+o(this.getSeconds(),2)+" "+this.getUTCOffset()});q("toYMD",function(s){s=typeof s==="undefined"?"-":s;return this.getFullYear()+s+o(this.getMonth()+1,2)+s+o(this.getDate(),2)});q("toDBString",function(){return this.getUTCFullYear()+"-"+o(this.getUTCMonth()+1,2)+"-"+o(this.getUTCDate(),2)+" "+o(this.getUTCHours(),2)+":"+o(this.getUTCMinutes(),2)+":"+o(this.getUTCSeconds(),2)});q("clearTime",function(){this.setHours(0);this.setMinutes(0);this.setSeconds(0);this.setMilliseconds(0);return this});q("clearUTCTime",function(){this.setUTCHours(0);this.setUTCMinutes(0);this.setUTCSeconds(0);this.setUTCMilliseconds(0);return this});q("add",function(s){if(s.milliseconds!==undefined){this.setMilliseconds(this.getMilliseconds()+s.milliseconds)}if(s.seconds!==undefined){this.setSeconds(this.getSeconds()+s.seconds)}if(s.minutes!==undefined){this.setMinutes(this.getMinutes()+s.minutes)}if(s.hours!==undefined){this.setHours(this.getHours()+s.hours)}if(s.days!==undefined){this.setDate(this.getDate()+s.days)}if(s.weeks!==undefined){this.setDate(this.getDate()+(s.weeks*7))}if(s.months!==undefined){this.setMonth(this.getMonth()+s.months)}if(s.real_months!==undefined){var t=this.getDate();this.setDate(1);this.setMonth(this.getMonth()+s.real_months);this.setDate(Math.min(t,Date.getDaysInMonth(this.getFullYear(),this.getMonth())))}if(s.years!==undefined){this.setFullYear(this.getFullYear()+s.years)}return this});q("addMilliseconds",function(s){return this.add({milliseconds:s})});q("addSeconds",function(s){return this.add({seconds:s})});q("addMinutes",function(s){return this.add({minutes:s})});q("addHours",function(s){return this.add({hours:s})});q("addDays",function(s){return this.add({days:s})});q("addWeeks",function(s){return this.add({days:(s*7)})});q("addMonths",function(s){return this.add({months:s})});q("addRealMonths",function(s){return this.add({real_months:s})});q("addYears",function(s){return this.add({years:s})});q("remove",function(s){if(s.seconds!==undefined){this.setSeconds(this.getSeconds()-s.seconds)}if(s.minutes!==undefined){this.setMinutes(this.getMinutes()-s.minutes)}if(s.hours!==undefined){this.setHours(this.getHours()-s.hours)}if(s.days!==undefined){this.setDate(this.getDate()-s.days)}if(s.weeks!==undefined){this.setDate(this.getDate()-(s.weeks*7))}if(s.months!==undefined){this.setMonth(this.getMonth()-s.months)}if(s.years!==undefined){this.setFullYear(this.getFullYear()-s.years)}return this});q("removeMilliseconds",function(s){throw new Error("Not implemented")});q("removeSeconds",function(s){return this.remove({seconds:s})});q("removeMinutes",function(s){return this.remove({minutes:s})});q("removeHours",function(s){return this.remove({hours:s})});q("removeDays",function(s){return this.remove({days:s})});q("removeWeeks",function(s){return this.remove({days:(s*7)})});q("removeMonths",function(s){return this.remove({months:s})});q("removeYears",function(s){return this.remove({years:s})});q("addWeekdays",function(s){var t=this.getDay();if(t===0){t=7}var u=s;var v=Math.floor((s+t-1)/5);if(s>0){u+=v*2;if(t>5){u-=t-5}}else{u+=Math.min(v*2,0);if(t>6){u-=1}}return this.addDays(u)});q("setTimeToNow",function(){var s=new Date();this.setMilliseconds(s.getMilliseconds());this.setSeconds(s.getSeconds());this.setMinutes(s.getMinutes());this.setHours(s.getHours())});q("clone",function(){return new Date(this.valueOf())});q("between",function(t,s){return(this.valueOf()>=t.valueOf()&&this.valueOf()<=s.valueOf())});q("compareTo",function(s){return Date.compare(this,s)});q("equals",function(s){return Date.equals(this,s)});q("equalsDay",function(s){return Date.equalsDay(this,s)});q("isToday",function(){return Date.equalsDay(this,Date.today())});q("isAfter",function(s){s=s?s:new Date();return(this.compareTo(s)>0)});q("isBefore",function(s){s=s?s:new Date();return(this.compareTo(s)<0)});q("isWeekend",function(s){return(this.getDay()%6===0)});q("getDaysBetween",function(s){return((s.clone().valueOf()-this.valueOf())/86400000)|0});q("getHoursBetween",function(s){return((s.clone().valueOf()-this.valueOf())/3600000)|0});q("getMinutesBetween",function(s){return((s.clone().valueOf()-this.valueOf())/60000)|0});q("getSecondsBetween",function(s){return((s.clone().valueOf()-this.valueOf())/1000)|0});q("getMillisecondsBetween",function(s){return((s.clone().valueOf()-this.valueOf()))|0 });q("getMonthsBetween",function(v){var x,u,t=Math.ceil(new Date(v-this).getUTCDate()/31),s=new Date(this.getTime()),w=Date.getDaysInMonth;s.setUTCMonth(s.getUTCMonth()+t);while(s.getTime()<v.getTime()){s.setUTCMonth(s.getUTCMonth()+1);t++}if(s.getTime()!==v.getTime()){s.setUTCMonth(s.getUTCMonth()-1);t--}if(v.getUTCMonth()===s.getUTCMonth()){x=new Date(v-s).getUTCDate();u=w(s.getUTCFullYear(),s.getUTCMonth());return t+(x/u)}else{u=w(s.getUTCFullYear(),s.getUTCMonth());x=u-s.getUTCDate()+1;return t+(+(x/u).toFixed(5))+(+(v.getUTCDate()/w(v.getUTCFullYear(),v.getUTCMonth())).toFixed(5))}});q("getOrdinalNumber",function(){return Math.ceil((this.clone().clearTime()-new Date(this.getFullYear(),0,1))/86400000)+1});q("toFormat",function(s){return a(s,d(this))});q("toUTCFormat",function(s){return a(s,m(this))});q("getWeekNumber",function(){var s=new Date(this.getFullYear(),0,1);return Math.ceil((((this-s)/86400000)+s.getDay()+1)/7)});q("getFullWeekNumber",function(){var s=""+this.getWeekNumber();if(s.length===1){s="0"+s}return s});var a=function(z,u){var y=[z],w,t,x;var v=function(F,D){var C=0,s=y.length,A,E,B,G=[];for(;C<s;C++){if(typeof y[C]=="string"){B=y[C].split(F);for(A=0,E=B.length-1;A<E;A++){G.push(B[A]);G.push([D])}G.push(B[E])}else{G.push(y[C])}}y=G};for(w in u){v(w,u[w])}x="";for(w=0,t=y.length;w<t;w++){x+=typeof y[w]=="string"?y[w]:y[w][0]}return y.join("")};var d=function(t){var s=(t.getHours()%12)?t.getHours()%12:12;return{"YYYY":t.getFullYear(),"YY":String(t.getFullYear()).slice(-2),"MMMM":e[t.getMonth()],"MMM":g[t.getMonth()],"MM":o(t.getMonth()+1,2),"MI":o(t.getMinutes(),2),"M":t.getMonth()+1,"DDDD":k[t.getDay()],"DDD":p[t.getDay()],"DD":o(t.getDate(),2),"D":t.getDate(),"HH24":o(t.getHours(),2),"HH":o(s,2),"H":s,"SS":o(t.getSeconds(),2),"PP":(t.getHours()>=12)?"PM":"AM","P":(t.getHours()>=12)?"pm":"am","LL":o(t.getMilliseconds(),3)}};var m=function(t){var s=(t.getUTCHours()%12)?t.getUTCHours()%12:12;return{"YYYY":t.getUTCFullYear(),"YY":String(t.getUTCFullYear()).slice(-2),"MMMM":e[t.getUTCMonth()],"MMM":g[t.getUTCMonth()],"MM":o(t.getUTCMonth()+1,2),"MI":o(t.getUTCMinutes(),2),"M":t.getUTCMonth()+1,"DDDD":k[t.getUTCDay()],"DDD":p[t.getUTCDay()],"DD":o(t.getUTCDate(),2),"D":t.getUTCDate(),"HH24":o(t.getUTCHours(),2),"HH":o(s,2),"H":s,"SS":o(t.getUTCSeconds(),2),"PP":(t.getUTCHours()>=12)?"PM":"AM","P":(t.getUTCHours()>=12)?"pm":"am","LL":o(t.getUTCMilliseconds(),3)}}}());