date-helper.min.js
9.1 KB
Date.gregorian_MD=[31,28,31,30,31,30,31,31,30,31,30,31],Date.local_MD=[31,31,31,31,31,31,30,30,30,30,30,29],Date.SECOND=1e3,Date.MINUTE=60*Date.SECOND,Date.HOUR=60*Date.MINUTE,Date.DAY=24*Date.HOUR,Date.WEEK=7*Date.DAY,Date.prototype.setLocalDateOnly=function(t,e){if("gregorian"!=t)return"";var a=new Date(e);this.setDate(1),this.setFullYear(a.getFullYear()),this.setMonth(a.getMonth()),this.setDate(a.getDate())},Date.prototype.setLocalDate=function(t,e){return"gregorian"!=t?this.setJalaliDate(e):this.setDate(e)},Date.prototype.setLocalMonth=function(t,e,a){return"gregorian"!=t?this.setJalaliMonth(e,a):(void 0==a&&this.getDate(),this.setMonth(e))},Date.prototype.setOtherFullYear=function(t,e){if("gregorian"!=t)return(a=new Date(this)).setLocalFullYear(e),a.getLocalMonth("jalali")!=this.getLocalMonth("jalali")&&this.setLocalDate("jalali",29),this.setLocalFullYear("jalali",e);var a=new Date(this);return a.setFullYear(e),a.getMonth()!=this.getMonth()&&this.setDate(28),this.setUTCFullYear(e)},Date.prototype.setLocalFullYear=function(t,e){if("gregorian"!=t)return this.setJalaliFullYear(e);var a=new Date(this);return a.setFullYear(e),a.getMonth()!=this.getMonth()&&this.setDate(28),this.setFullYear(e)},Date.prototype.getLocalWeekDays=function(t,e){return 6},Date.prototype.getOtherFullYear=function(t){return"gregorian"!=t?this.getJalaliFullYear():this.getFullYear()},Date.prototype.getLocalFullYear=function(t){return"gregorian"!=t?this.getJalaliFullYear():this.getFullYear()},Date.prototype.getLocalMonth=function(t){return"gregorian"!=t?this.getJalaliMonth():this.getMonth()},Date.prototype.getLocalDate=function(t){return"gregorian"!=t?this.getJalaliDate():this.getDate()},Date.prototype.getLocalDay=function(t){return"gregorian"!=t?this.getJalaliDay():this.getDay()},Date.prototype.getLocalMonthDays=function(t,e){if("gregorian"==t){a=this.getFullYear();return void 0===e&&(e=this.getMonth()),0!=a%4||0==a%100&&0!=a%400||1!=e?Date.gregorian_MD[e]:29}var a=this.getLocalFullYear("jalali");if(void 0===e&&(e=this.getLocalMonth("jalali")),0==a%4&&(0!=a%100||0==a%400)&&1==e)return 29;Date.local_MD[e]},Date.prototype.getLocalWeekNumber=function(t){if("gregorian"!=t){a=(e=new Date(this.getFullYear(),this.getMonth(),this.getDate(),0,0,0)).getDay();e.setDate(e.getDate()-(a+6)%7+3);r=e.valueOf();return e.setMonth(0),e.setDate(4),Math.round((r-e.valueOf())/6048e5)+1}var e=new Date(this.getFullYear(),this.getMonth(),this.getDate(),0,0,0),a=e.getDay();e.setDate(e.getDate()-(a+6)%7+3);var r=e.valueOf();return e.setMonth(0),e.setDate(4),Math.round((r-e.valueOf())/6048e5)+1},Date.prototype.getLocalDayOfYear=function(t){if("gregorian"!=t){r=(e=new Date(this.getOtherFullYear(t),this.getLocalMonth(t),this.getLocalDate(t),0,0,0))-(a=new Date(this.getOtherFullYear(t),0,0,0,0,0));return Math.floor(r/Date.DAY)}var e=new Date(this.getFullYear(),this.getMonth(),this.getDate(),0,0,0),a=new Date(this.getFullYear(),0,0,0,0,0),r=e-a;return Math.floor(r/Date.DAY)},Date.prototype.getMonthDays=function(t){var e=this.getFullYear();return void 0===t&&(t=this.getMonth()),0!=e%4||0==e%100&&0!=e%400||1!=t?"gregorian"==Date.dateType?Date.gregorian_MD[t]:void Date.local_MD[t]:29},Date.prototype.equalsTo=function(t){return this.getFullYear()==t.getFullYear()&&this.getMonth()==t.getMonth()&&this.getDate()==t.getDate()&&this.getHours()==t.getHours()&&this.getMinutes()==t.getMinutes()},Date.localCalToGregorian=function(t,e,a){return JalaliDate.jalaliToGregorian(t,e,a)},Date.gregorianToLocalCal=function(t,e,a){return JalaliDate.gregorianToJalali(t,e,a)},Date.numbersToIso=function(t){var e,a=[0,1,2,3,4,5,6,7,8,9];if(t=t.toString(),"[object Array]"===Object.prototype.toString.call(JoomlaCalLocale.localLangNumbers))for(e=0;e<a.length;e++)t=t.replace(new RegExp(JoomlaCalLocale.localLangNumbers[e],"g"),a[e]);return t},Date.prototype.print=function(t,e,a){if("string"!=typeof e&&(t=""),e||(e="gregorian"),"string"!=typeof t&&(t=""),!t)return"";if("NaN"==this.getLocalDate(e)||!this.getLocalDate(e))return"";var r=this.getLocalMonth(e),o=this.getLocalDate(e),l=this.getLocalFullYear(e),i=this.getLocalWeekNumber(e),n=this.getLocalDay(e),s={},g=this.getHours(),h=g>=12,D=h?g-12:g,u=this.getLocalDayOfYear(e);0==D&&(D=12);var c=this.getMinutes(),p=this.getSeconds();s["%a"]=JoomlaCalLocale.shortDays[n],s["%A"]=JoomlaCalLocale.days[n],s["%b"]=JoomlaCalLocale.shortMonths[r],s["%B"]=JoomlaCalLocale.months[r],s["%C"]=1+Math.floor(l/100),s["%d"]=o<10?"0"+o:o,s["%e"]=o,s["%H"]=g<10?"0"+g:g,s["%I"]=D<10?"0"+D:D,s["%j"]=u<100?u<10?"00"+u:"0"+u:u,s["%k"]=g,s["%l"]=D,s["%m"]=r<9?"0"+(1+r):1+r,s["%M"]=c<10?"0"+c:c,s["%n"]="\n",s["%p"]=h?JoomlaCalLocale.PM:JoomlaCalLocale.AM,s["%P"]=h?JoomlaCalLocale.pm:JoomlaCalLocale.am,s["%s"]=Math.floor(this.getTime()/1e3),s["%S"]=p<10?"0"+p:p,s["%t"]="\t",s["%U"]=s["%W"]=s["%V"]=i<10?"0"+i:i,s["%u"]=n+1,s["%w"]=n,s["%y"]=(""+l).substr(2,2),s["%Y"]=l,s["%%"]="%";var f=/%./g,M=t.replace(f,function(t){return s[t]||t});return"[object Array]"===Object.prototype.toString.call(JoomlaCalLocale.localLangNumbers)&&a&&(M=Date.convertNumbers(M)),M},Date.parseFieldDate=function(t,e,a){t=Date.numbersToIso(t);var r=new Date,o=0,l=-1,i=0,n=t.split(/\W+/),s=e.match(/%./g),g=0,h=0,D=0,u=0;for(g=0;g<n.length;++g)if(n[g])switch(s[g]){case"%d":case"%e":i=parseInt(n[g],10);break;case"%m":l=parseInt(n[g],10)-1;break;case"%Y":case"%y":(o=parseInt(n[g],10))<100&&(o+=o>29?1900:2e3);break;case"%b":case"%B":for(h=0;h<12;++h)if("gregorian"!=a){if(JoomlaCalLocale.months[h].substr(0,n[g].length).toLowerCase()==n[g].toLowerCase()){l=h;break}}else if(JoomlaCalLocale.months[h].substr(0,n[g].length).toLowerCase()==n[g].toLowerCase()){l=h;break}break;case"%H":case"%I":case"%k":case"%l":D=parseInt(n[g],10);break;case"%P":case"%p":/pm/i.test(n[g])&&D<12?D+=12:/am/i.test(n[g])&&D>=12&&(D-=12);break;case"%M":u=parseInt(n[g],10);break;case"%S":sec=parseInt(n[g],10)}if(isNaN(o)&&(o=r.getFullYear()),isNaN(l)&&(l=r.getMonth()),isNaN(i)&&(i=r.getDate()),isNaN(D)&&(D=r.getHours()),isNaN(u)&&(u=r.getMinutes()),0!=o&&-1!=l&&0!=i)return new Date(o,l,i,D,u,0);for(o=0,l=-1,i=0,g=0;g<n.length;++g)if(-1!=n[g].search(/[a-zA-Z]+/)){var c=-1;for(h=0;h<12;++h)if("gregorian"!=a){if(JoomlaCalLocale.months[h].substr(0,n[g].length).toLowerCase()==n[g].toLowerCase()){c=h;break}}else if(JoomlaCalLocale.months[h].substr(0,n[g].length).toLowerCase()==n[g].toLowerCase()){c=h;break}-1!=c&&(-1!=l&&(i=l+1),l=c)}else parseInt(n[g],10)<=12&&-1==l?l=n[g]-1:parseInt(n[g],10)>31&&0==o?(o=parseInt(n[g],10))<100&&(o+=o>29?1900:2e3):0==i&&(i=n[g]);return 0==o&&(o=r.getFullYear()),-1!=l&&0!=i?new Date(o,l,i,D,u,0):r},JalaliDate={g_days_in_month:[31,28,31,30,31,30,31,31,30,31,30,31],j_days_in_month:[31,31,31,31,31,31,30,30,30,30,30,29]},JalaliDate.jalaliToGregorian=function(t,e,a){for(var r=(t=parseInt(t))-979,o=(e=parseInt(e))-1,l=(a=parseInt(a))-1,i=365*r+8*parseInt(r/33)+parseInt((r%33+3)/4),n=0;n<o;++n)i+=JalaliDate.j_days_in_month[n];var s=(i+=l)+79,g=1600+400*parseInt(s/146097),h=!0;(s%=146097)>=36525&&(s--,g+=100*parseInt(s/36524),(s%=36524)>=365?s++:h=!1),g+=4*parseInt(s/1461),(s%=1461)>=366&&(h=!1,s--,g+=parseInt(s/365),s%=365);for(n=0;s>=JalaliDate.g_days_in_month[n]+(1==n&&h);n++)s-=JalaliDate.g_days_in_month[n]+(1==n&&h);return[g,n+1,s+1]},JalaliDate.checkDate=function(t,e,a){return!(t<0||t>32767||e<1||e>12||a<1||a>JalaliDate.j_days_in_month[e-1]+(12==e&&!((t-979)%33%4)))},JalaliDate.gregorianToJalali=function(t,e,a){for(var r=(t=parseInt(t))-1600,o=(e=parseInt(e))-1,l=(a=parseInt(a))-1,i=365*r+parseInt((r+3)/4)-parseInt((r+99)/100)+parseInt((r+399)/400),n=0;n<o;++n)i+=JalaliDate.g_days_in_month[n];o>1&&(r%4==0&&r%100!=0||r%400==0)&&++i;var s=(i+=l)-79,g=parseInt(s/12053);s%=12053;var h=979+33*g+4*parseInt(s/1461);(s%=1461)>=366&&(h+=parseInt((s-1)/365),s=(s-1)%365);for(n=0;n<11&&s>=JalaliDate.j_days_in_month[n];++n)s-=JalaliDate.j_days_in_month[n];return[h,n+1,s+1]},Date.prototype.setJalaliFullYear=function(t,e,a){var r=this.getDate(),o=this.getMonth(),l=this.getFullYear(),i=JalaliDate.gregorianToJalali(l,o+1,r);t<100&&(t+=1300),i[0]=t,void 0!=e&&(e>11&&(i[0]+=Math.floor(e/12),e%=12),i[1]=e+1),void 0!=a&&(i[2]=a);var n=JalaliDate.jalaliToGregorian(i[0],i[1],i[2]);return this.setFullYear(n[0],n[1]-1,n[2])},Date.prototype.setJalaliMonth=function(t,e){var a=this.getDate(),r=this.getMonth(),o=this.getFullYear(),l=JalaliDate.gregorianToJalali(o,r+1,a);t>11&&(l[0]+=Math.floor(t/12),t%=12),l[1]=t+1,void 0!=e&&(l[2]=e);var i=JalaliDate.jalaliToGregorian(l[0],l[1],l[2]);return this.setFullYear(i[0],i[1]-1,i[2])},Date.prototype.setJalaliDate=function(t){var e=this.getDate(),a=this.getMonth(),r=this.getFullYear(),o=JalaliDate.gregorianToJalali(r,a+1,e);o[2]=t;var l=JalaliDate.jalaliToGregorian(o[0],o[1],o[2]);return this.setFullYear(l[0],l[1]-1,l[2])},Date.prototype.getJalaliFullYear=function(){var t=this.getDate(),e=this.getMonth(),a=this.getFullYear();return JalaliDate.gregorianToJalali(a,e+1,t)[0]},Date.prototype.getJalaliMonth=function(){var t=this.getDate(),e=this.getMonth(),a=this.getFullYear();return JalaliDate.gregorianToJalali(a,e+1,t)[1]-1},Date.prototype.getJalaliDate=function(){var t=this.getDate(),e=this.getMonth(),a=this.getFullYear();return JalaliDate.gregorianToJalali(a,e+1,t)[2]},Date.prototype.getJalaliDay=function(){var t=this.getDay();return t%=7};