diff --git a/bower.json b/bower.json index a39280d..c2a463d 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "persian-date", - "version": "0.2.2", + "version": "0.2.3", "main": [ "dist/<= pkg.version>/persian-date-<= pkg.version>.js" ], diff --git a/dist/persian-date.js b/dist/persian-date.js index 8d1be61..7e20afe 100644 --- a/dist/persian-date.js +++ b/dist/persian-date.js @@ -1,6 +1,6 @@ /*! * - * persian-date - 0.2.2 + * persian-date - 0.2.3 * Reza Babakhani * http://babakhani.github.io/PersianWebToolkit/docs/persian-date/ * Under WTFPL license @@ -510,7 +510,7 @@ var PersianDateClass = function () { this.gDate = new Date(); } this.pDate = this.algorithms.toPersianDate(this.gDate); - this.version = "0.2.2"; + this.version = "0.2.3"; this.formatPersian = '_default'; this._utcMode = false; return this; @@ -1648,6 +1648,8 @@ var Algorithms = function () { key: 'isLeapPersian', value: function isLeapPersian(year) { return ((year - (year > 0 ? 474 : 473)) % 2820 + 474 + 38) * 682 % 2816 < 682; + // https://fa.wikipedia.org/wiki/%DA%AF%D8%A7%D9%87%E2%80%8C%D8%B4%D9%85%D8%A7%D8%B1%DB%8C_%D9%87%D8%AC%D8%B1%DB%8C_%D8%AE%D9%88%D8%B1%D8%B4%DB%8C%D8%AF%DB%8C_%D8%AD%D8%B3%D8%A7%D8%A8%DB%8C + // return parseFloat('0.' + ((year + 2346) * (0.24219858156)).toString().split('.')[1]) < 0.24219858156; } /** @@ -1837,9 +1839,15 @@ var Algorithms = function () { }, { key: 'persianArrayToGregorianDate', value: function persianArrayToGregorianDate(parray) { + if (parray[1] === undefined) { + parray[1] = 1; + } + if (parray[2] === undefined) { + parray[2] = 1; + } // Howha : javascript Cant Parse this array truly 2011,2,20 - var pd = this.calcPersian(parray[0] ? parray[0] : 0, parray[1] ? parray[1] : 1, parray[2] ? parray[2] : 1), - gDate = new Date(pd[0], pd[1], pd[2]); + var pd = this.calcPersian(parray[0], parray[1], parray[2]), + gDate = new Date(); gDate.setYear(pd[0]); gDate.setMonth(pd[1]); gDate.setDate(pd[2]); diff --git a/dist/persian-date.min.js b/dist/persian-date.min.js index bcc5269..174c3db 100644 --- a/dist/persian-date.min.js +++ b/dist/persian-date.min.js @@ -1,10 +1,10 @@ /*! * - * persian-date - 0.2.2 + * persian-date - 0.2.3 * Reza Babakhani * http://babakhani.github.io/PersianWebToolkit/docs/persian-date/ * Under WTFPL license * * */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.persianDate=t():e.persianDate=t()}(this,function(){return function(e){function t(r){if(a[r])return a[r].exports;var n=a[r]={i:r,l:!1,exports:{}};return e[r].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var a={};return t.m=e,t.c=a,t.i=function(e){return e},t.d=function(e,a,r){t.o(e,a)||Object.defineProperty(e,a,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var a=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(a,"a",a),a},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=5)}([function(e,t,a){"use strict";e.exports={durationUnit:{year:["y","years","year"],month:["M","months","month"],day:["d","days","day"],hour:["h","hours","hour"],minute:["m","minutes","minute"],second:["s","second","seconds"],millisecond:["ms","milliseconds","millisecond"],week:["w","","weeks","week"]},GREGORIAN_EPOCH:1721425.5,PERSIAN_EPOCH:1948320.5,monthRange:{1:{name:{fa:"فروردین"},abbr:{fa:"فرو"}},2:{name:{fa:"اردیبهشت"},abbr:{fa:"ارد"}},3:{name:{fa:"خرداد"},abbr:{fa:"خرد"}},4:{name:{fa:"تیر"},abbr:{fa:"تیر"}},5:{name:{fa:"مرداد"},abbr:{fa:"مرد"}},6:{name:{fa:"شهریور"},abbr:{fa:"شهر"}},7:{name:{fa:"مهر"},abbr:{fa:"مهر"}},8:{name:{fa:"آبان"},abbr:{fa:"آبا"}},9:{name:{fa:"آذر"},abbr:{fa:"آذر"}},10:{name:{fa:"دی"},abbr:{fa:"دی"}},11:{name:{fa:"بهمن"},abbr:{fa:"بهم"}},12:{name:{fa:"اسفند"},abbr:{fa:"اسف"}}},weekRange:{1:{name:{fa:"شنبه"},abbr:{fa:"ش"}},2:{name:{fa:"یکشنبه"},abbr:{fa:"ی"}},3:{name:{fa:"دوشنبه"},abbr:{fa:"د"}},4:{name:{fa:"سه شنبه"},abbr:{fa:"س"}},5:{name:{fa:"چهار شنبه"},abbr:{fa:"چ"}},6:{name:{fa:"پنج شنبه"},abbr:{fa:"پ"}},0:{name:{fa:"جمعه"},abbr:{fa:"ج"}}},persianDaysName:["اورمزد","بهمن","اوردیبهشت","شهریور","سپندارمذ","خورداد","امرداد","دی به آذز","آذز","آبان","خورشید","ماه","تیر","گوش","دی به مهر","مهر","سروش","رشن","فروردین","بهرام","رام","باد","دی به دین","دین","ارد","اشتاد","آسمان","زامیاد","مانتره سپند","انارام","زیادی"]}},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var a=0;a-1?e="year":i.month.indexOf(e)>-1?e="month":i.day.indexOf(e)>-1?e="day":i.hour.indexOf(e)>-1?e="hour":i.minute.indexOf(e)>-1?e="minute":i.second.indexOf(e)>-1&&(e="second"),{unit:e,value:t}}},{key:"absRound",value:function(e){return e<0?Math.ceil(e):Math.floor(e)}},{key:"mod",value:function(e,t){return e-t*Math.floor(e/t)}}]),e}();e.exports=s},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var a=0;a0){var n=this.year()+r.years;this.year(n)}if(r.months>0){var i=this.month()+r.months;this.month(i)}if(r.days>0){var s=this.date()+r.days;this.date(s)}if(r.hours>0){var u=this.hour()+r.hours;this.hour(u)}if(r.minutes>0){var h=this.minute()+r.minutes;this.minute(h)}if(r.seconds>0){var c=this.second()+r.seconds;this.second(c)}if(r.milliseconds>0){var f=this.milliseconds()+r.milliseconds;this.milliseconds(f)}return new t(this.valueOf())}},{key:"subtract",value:function(e,a){var r=new o(e,a)._data;if(r.years>0){var n=this.year()-r.years;this.year(n)}if(r.months>0){var i=this.month()-r.months;this.month(i)}if(r.days>0){var s=this.date()-r.days;this.date(s)}if(r.hours>0){var u=this.hour()-r.hours;this.hour(u)}if(r.minutes>0){var h=this.minute()-r.minutes;this.minute(h)}if(r.seconds>0){var c=this.second()-r.seconds;this.second(c)}if(r.milliseconds>0){var f=this.milliseconds()-r.milliseconds;this.milliseconds(f)}return new t(this.valueOf())}},{key:"formatNumber",value:function(){var t=void 0,a=this;return"_default"===this.formatPersian?t=void 0!==e&&void 0!==e.exports?a.formatPersian!==!1:window.formatPersian!==!1:this.formatPersian===!0?t=!0:this.formatPersian===!1?t=!1:Error('Invalid Config "formatPersian" !!'),t}},{key:"format",value:function(e){function t(e){switch(e){case"a":return i?n.hour>=12?"ب ظ":"ق ظ":n.hour>=12?"PM":"AM";case"H":return s(n.hour);case"HH":return s(h(n.hour,2));case"h":return s(n.hour%12);case"hh":return s(h(n.hour%12,2));case"m":case"mm":return s(h(n.minute,2));case"s":return s(n.second);case"ss":return s(h(n.second,2));case"D":return s(h(n.date));case"DD":return s(h(n.date,2));case"DDD":var t=a.startOf("year");return s(h(a.diff(t,"days"),3));case"DDDD":var r=a.startOf("year");return s(h(a.diff(r,"days"),3));case"d":return s(a.pDate.weekDayNumber);case"ddd":return c[a.pDate.weekDayNumber].abbr.fa;case"dddd":return c[a.pDate.weekDayNumber].name.fa;case"ddddd":return f[a.pDate.monthDayNumber];case"w":var o=a.startOf("year"),u=parseInt(a.diff(o,"days")/7)+1;return s(u);case"ww":var l=a.startOf("year"),m=h(parseInt(a.diff(l,"days")/7)+1,2);return s(m);case"M":return s(n.month);case"MM":return s(h(n.month,2));case"MMM":return d[n.month].abbr.fa;case"MMMM":return d[n.month].name.fa;case"YY":var y=n.year.toString().split("");return s(y[2]+y[3]);case"YYYY":return s(n.year);case"Z":var v="+",D=Math.round(n.timezone/60),p=n.timezone%60;p<0&&(p*=-1),D<0&&(v="-",D*=-1);var g=v+h(D,2)+":"+h(p,2);return s(g);case"ZZ":var w="+",k=Math.round(n.timezone/60),b=n.timezone%60;b<0&&(b*=-1),k<0&&(w="-",k*=-1);var M=w+h(k,2)+""+h(b,2);return s(M);case"X":return a.unix();case"LT":return a.format("h:m a");case"L":return a.format("YYYY/MM/DD");case"l":return a.format("YYYY/M/D");case"LL":return a.format("MMMM DD YYYY");case"ll":return a.format("MMM DD YYYY");case"LLL":return a.format("MMMM YYYY DD h:m a");case"lll":return a.format("MMM YYYY DD h:m a");case"LLLL":return a.format("dddd D MMMM YYYY h:m a");case"llll":return a.format("ddd D MMM YYYY h:m a")}}var a=this,r=/([[^[]*])|(\\)?(Mo|MM?M?M?|Do|DD?D?D?|ddddd|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|X|LT|ll?l?l?|LL?L?L?)/g,n={year:a.year(),month:a.month(),hour:a.hours(),minute:a.minutes(),second:a.seconds(),date:a.date(),timezone:a.zone(),unix:a.unix()},i=a.formatNumber(),s=function(e){return i?u(e):e};return e?e.replace(r,t):"YYYY-MM-DD HH:mm:ss a".replace(r,t)}},{key:"diff",value:function(e,t,a){var r=this,n=e,i=r.gDate-n.gDate-0,s=r.year()-n.year(),o=r.month()-n.month(),u=(r.date()-n.date())*-1,h=void 0;return h="months"===t||"month"===t?12*s+o+u/30:"years"===t||"year"===t?s+(o+u/30)/12:"seconds"===t||"second"===t?i/1e3:"minutes"===t||"minute"===t?i/6e4:"hours"===t||"hour"===t?i/36e5:"days"===t||"day"===t?i/864e5:"weeks"===t||"week"===t?i/6048e5:i,h<0&&(h*=-1),a?h:Math.round(h)}},{key:"startOf",value:function(e){switch(e){case"years":case"year":return new t([this.year(),1,1]);case"months":case"month":return new t([this.year(),this.month(),1]);case"days":case"day":return new t([this.year(),this.month(),this.date(),0,0,0]);case"hours":case"hour":return new t([this.year(),this.month(),this.date(),this.hours(),0,0]);case"minutes":case"minute":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),0]);case"seconds":case"second":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),this.seconds()]);case"weeks":case"week":var a=this.pDate.weekDayNumber;return 0===a?new t([this.year(),this.month(),this.date()]):new t([this.year(),this.month(),this.date()]).subtract("days",a);default:return this}}},{key:"endOf",value:function(e){switch(e){case"years":case"year":var a=this.isLeapYear()?30:29;return new t([this.year(),12,a,23,59,59]);case"months":case"month":var r=this.daysInMonth(this.year(),this.month());return new t([this.year(),this.month(),r,23,59,59]);case"days":case"day":return new t([this.year(),this.month(),this.date(),23,59,59]);case"hours":case"hour":return new t([this.year(),this.month(),this.date(),this.hours(),59,59]);case"minutes":case"minute":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),59]);case"seconds":case"second":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),this.seconds()]);case"weeks":case"week":var n=this.pDate.weekDayNumber;return n=6===n?7:6-n,new t([this.year(),this.month(),this.date()]).add("days",n);default:return this}}},{key:"sod",value:function(){return this.startOf("day")}},{key:"eod",value:function(){return this.endOf("day")}},{key:"zone",value:function(){return this.pDate.timeZoneOffset}},{key:"local",value:function(){var e=void 0;if(this._utcMode){var t=60*this.pDate.timeZoneOffset*1e3;return e=this.pDate.timeZoneOffset<0?this.valueOf()-t:this.valueOf()+t,this.gDate=new Date(e),this._updatePDate(),this._utcMode=!1,this}return this}},{key:"utc",value:function(e){var a=void 0;if(e)return new t(e).utc();if(this._utcMode)return this;var r=60*this.pDate.timeZoneOffset*1e3;return a=this.pDate.timeZoneOffset<0?this.valueOf()+r:this.valueOf()-r,this.gDate=new Date(a),this._updatePDate(),this._utcMode=!0,this}},{key:"isUtc",value:function(){return this._utcMode}},{key:"isDST",value:function(){var e=this.month(),t=this.date();return!(e<7)&&(7==e&&t>=2||e>=7||void 0)}},{key:"isLeapYear",value:function(){return this.algorithms.isLeapPersian(this.year())}},{key:"daysInMonth",value:function(e,t){var a=e||this.year(),r=t||this.month();return r<1||r>12?0:r<7?31:r<12?30:this.algorithms.isLeapPersian(a)?30:29}},{key:"toDate",value:function(){return this.gDate}},{key:"toArray",value:function(){return[this.year(),this.month(),this.date(),this.hour(),this.minute(),this.second(),this.millisecond()]}},{key:"_valueOf",value:function(){return this.gDate.valueOf()}},{key:"unix",value:function(e){var a=void 0;if(e)return new t(1e3*e);var r=this.gDate.valueOf().toString();return a=r.substring(0,r.length-3),parseInt(a)}},{key:"isPersianDate",value:function(e){return e instanceof t}},{key:"millisecond",value:function(e){return this.milliseconds(e)}},{key:"milliseconds",value:function(e){return e?(this.gDate.setMilliseconds(e),this._updatePDate(),this):this.pDate.milliseconds}},{key:"second",value:function(e){return this.seconds(e)}},{key:"seconds",value:function(e){return e|0===e?(this.gDate.setSeconds(e),this._updatePDate(),this):this.pDate.seconds}},{key:"minute",value:function(e){return this.minutes(e)}},{key:"minutes",value:function(e){return e||0===e?(this.gDate.setMinutes(e),this._updatePDate(),this):parseInt(this.pDate.minutes)}},{key:"hour",value:function(e){return this.hours(e)}},{key:"hours",value:function(e){return e|0===e?(this.gDate.setHours(e),this._updatePDate(),this):this.pDate.hours}},{key:"dates",value:function(e){return this.date(e)}},{key:"date",value:function(e){if(e||0===e){var t=this.algorithms.getPersianArrayFromPDate(this.pDate);return t[2]=e,this.gDate=this.algorithms.persianArrayToGregorianDate(t),this._updatePDate(),this}return this.pDate.date}},{key:"days",value:function(){return this.day()}},{key:"day",value:function(){return this.pDate.day}},{key:"month",value:function(e){if(e|0===e){var t=this.algorithms.getPersianArrayFromPDate(this.pDate);return t[1]=e,this.gDate=this.algorithms.persianArrayToGregorianDate(t),this._updatePDate(),this}return this.pDate.month}},{key:"years",value:function(e){return this.year(e)}},{key:"year",value:function(e){if(e|0===e){var t=this.algorithms.getPersianArrayFromPDate(this.pDate);return t[0]=e,this.gDate=this.algorithms.persianArrayToGregorianDate(t),this._updatePDate(),this}return this.pDate.year}},{key:"getFirstWeekDayOfMonth",value:function(e,t){var a=this.algorithms.calcPersian(e,t,1),r=this.algorithms.calcGregorian(a[0],a[1],a[2]);return r[3]+2===8?1:r[3]+2===7?7:r[3]+2}},{key:"clone",value:function(){return new t(this.gDate)}},{key:"_updatePDate",value:function(){this.pDate=this.algorithms.toPersianDate(this.gDate)}},{key:"valueOf",value:function(){return this._valueOf()}}],[{key:"_utc",value:function(e){return e?new t(e).utc():(new t).utc()}},{key:"_unix",value:function(e){return e?new t(1e3*e).unix():(new t).unix()}}]),t}();e.exports=l},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var a=0;a0?474:473))%2820+474+38)%2816<682}},{key:"gregorianToJd",value:function(e,t,a){return i-1+365*(e-1)+Math.floor((e-1)/4)+-Math.floor((e-1)/100)+Math.floor((e-1)/400)+Math.floor((367*t-362)/12+(t<=2?0:this.isLeapGregorian(e)?-1:-2)+a)}},{key:"jdToGregorian",value:function(e){var t=Math.floor(e-.5)+.5,a=t-i,r=Math.floor(a/146097),n=u(a,146097),s=Math.floor(n/36524),o=u(n,36524),h=Math.floor(o/1461),c=u(o,1461),f=Math.floor(c/365),d=400*r+100*s+4*h+f;4!=s&&4!=f&&d++;var l=t-this.gregorianToJd(d,1,1),m=t=0?474:473),n=474+u(r,2820),a+(t<=7?31*(t-1):30*(t-1)+6)+Math.floor((682*n-110)/2816)+365*(n-1)+1029983*Math.floor(r/2820)+(s-1)}},{key:"jdToPersian",value:function(e){var t=void 0,a=void 0,r=void 0,n=void 0,i=void 0,s=void 0,o=void 0,h=void 0,c=void 0,f=void 0;return e=Math.floor(e)+.5,n=e-this.persianToJd(475,1,1),i=Math.floor(n/1029983),s=u(n,1029983),1029982===s?o=2820:(h=Math.floor(s/366),c=u(s,366),o=Math.floor((2134*h+2816*c+2815)/1028522)+h+1),t=o+2820*i+474,t<=0&&(t-=1),f=e-this.persianToJd(t,1,1)+1,a=f<=186?Math.ceil(f/31):Math.ceil((f-6)/30),r=e-this.persianToJd(t,a,1)+1,new Array(t,a,r)}},{key:"calcPersian",value:function(e,t,a){var r=this.persianToJd(e,t,a),n=this.jdToGregorian(r);return new Array(n[0],n[1]-1,n[2])}},{key:"calcGregorian",value:function(e,t,a){var r=this.gregorianToJd(e,t+1,a)+Math.floor(.5)/86400,n=this.jdToPersian(r),i=this.jwday(r);return new Array(n[0],n[1],n[2],i)}},{key:"toPersianDate",value:function(e){var t=this.calcGregorian(e.getFullYear(),e.getMonth(),e.getDate()),a={};return a.monthDayNumber=t[2]-1,6==t[3]?a.weekDayNumber=1:5===t[3]?a.weekDayNumber=0:4===t[3]?a.weekDayNumber=6:3===t[3]?a.weekDayNumber=5:2===t[3]?a.weekDayNumber=4:1===t[3]?a.weekDayNumber=3:0===t[3]&&(a.weekDayNumber=2),a.year=t[0],a.month=t[1],a.day=a.weekDayNumber,a.date=t[2],a.hours=e.getHours(),a.minutes=e.getMinutes()<10?"0"+e.getMinutes():e.getMinutes(),a.seconds=e.getSeconds(),a.milliseconds=e.getMilliseconds(),a.timeZoneOffset=e.getTimezoneOffset(),a}},{key:"persianArrayToGregorianDate",value:function(e){var t=this.calcPersian(e[0]?e[0]:0,e[1]?e[1]:1,e[2]?e[2]:1),a=new Date(t[0],t[1],t[2]);return a.setYear(t[0]),a.setMonth(t[1]),a.setDate(t[2]),a.setHours(e[3]?e[3]:0),a.setMinutes(e[4]?e[4]:0),a.setSeconds(e[5]?e[5]:0),a.setMilliseconds(e[6]?e[6]:0),a}},{key:"getPersianArrayFromPDate",value:function(e){return[e.year,e.month,e.date,e.hours,e.minutes,e.seconds,e.milliseconds]}}]),e}();e.exports=h},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=a(1),i=(new n).normalizeDuration,s=(new n).absRound,o=function e(t,a){r(this,e);var n={},o=this._data={},u=0,h=i(t,a);n[h.unit]=h.value,u=n.milliseconds||n.millisecond||n.ms||0;var c=n.years||n.year||n.y||0,f=n.months||n.month||n.M||0,d=n.weeks||n.w||n.week||0,l=n.days||n.d||n.day||0,m=n.hours||n.hour||n.h||0,y=n.minutes||n.minute||n.m||0,v=n.seconds||n.second||n.s||0;return this._milliseconds=u+1e3*v+6e4*y+36e5*m,this._days=l+7*d,this._months=f+12*c,o.milliseconds=u%1e3,v+=u/1e3,o.seconds=v%60,y+=s(v/60),o.minutes=y%60,m+=s(y/60),o.hours=m%24,l+=s(m/24),l+=7*d,o.days=l%30,f+=s(l/30),o.months=f%12,c+=s(f/12),o.years=c,this};o.prototype={valueOf:function(){return this._milliseconds+864e5*this._days+2592e6*this._months}},e.exports=o},function(e,t,a){"use strict";var r=a(2);String.prototype.toPersianDigit=function(e){return this.replace(/\d+/g,function(t){var a=[],r=[],n=void 0,i=void 0;for(n=0;n-1?e="year":i.month.indexOf(e)>-1?e="month":i.day.indexOf(e)>-1?e="day":i.hour.indexOf(e)>-1?e="hour":i.minute.indexOf(e)>-1?e="minute":i.second.indexOf(e)>-1&&(e="second"),{unit:e,value:t}}},{key:"absRound",value:function(e){return e<0?Math.ceil(e):Math.floor(e)}},{key:"mod",value:function(e,t){return e-t*Math.floor(e/t)}}]),e}();e.exports=s},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var a=0;a0){var n=this.year()+r.years;this.year(n)}if(r.months>0){var i=this.month()+r.months;this.month(i)}if(r.days>0){var s=this.date()+r.days;this.date(s)}if(r.hours>0){var u=this.hour()+r.hours;this.hour(u)}if(r.minutes>0){var h=this.minute()+r.minutes;this.minute(h)}if(r.seconds>0){var c=this.second()+r.seconds;this.second(c)}if(r.milliseconds>0){var f=this.milliseconds()+r.milliseconds;this.milliseconds(f)}return new t(this.valueOf())}},{key:"subtract",value:function(e,a){var r=new o(e,a)._data;if(r.years>0){var n=this.year()-r.years;this.year(n)}if(r.months>0){var i=this.month()-r.months;this.month(i)}if(r.days>0){var s=this.date()-r.days;this.date(s)}if(r.hours>0){var u=this.hour()-r.hours;this.hour(u)}if(r.minutes>0){var h=this.minute()-r.minutes;this.minute(h)}if(r.seconds>0){var c=this.second()-r.seconds;this.second(c)}if(r.milliseconds>0){var f=this.milliseconds()-r.milliseconds;this.milliseconds(f)}return new t(this.valueOf())}},{key:"formatNumber",value:function(){var t=void 0,a=this;return"_default"===this.formatPersian?t=void 0!==e&&void 0!==e.exports?a.formatPersian!==!1:window.formatPersian!==!1:this.formatPersian===!0?t=!0:this.formatPersian===!1?t=!1:Error('Invalid Config "formatPersian" !!'),t}},{key:"format",value:function(e){function t(e){switch(e){case"a":return i?n.hour>=12?"ب ظ":"ق ظ":n.hour>=12?"PM":"AM";case"H":return s(n.hour);case"HH":return s(h(n.hour,2));case"h":return s(n.hour%12);case"hh":return s(h(n.hour%12,2));case"m":case"mm":return s(h(n.minute,2));case"s":return s(n.second);case"ss":return s(h(n.second,2));case"D":return s(h(n.date));case"DD":return s(h(n.date,2));case"DDD":var t=a.startOf("year");return s(h(a.diff(t,"days"),3));case"DDDD":var r=a.startOf("year");return s(h(a.diff(r,"days"),3));case"d":return s(a.pDate.weekDayNumber);case"ddd":return c[a.pDate.weekDayNumber].abbr.fa;case"dddd":return c[a.pDate.weekDayNumber].name.fa;case"ddddd":return f[a.pDate.monthDayNumber];case"w":var o=a.startOf("year"),u=parseInt(a.diff(o,"days")/7)+1;return s(u);case"ww":var l=a.startOf("year"),m=h(parseInt(a.diff(l,"days")/7)+1,2);return s(m);case"M":return s(n.month);case"MM":return s(h(n.month,2));case"MMM":return d[n.month].abbr.fa;case"MMMM":return d[n.month].name.fa;case"YY":var y=n.year.toString().split("");return s(y[2]+y[3]);case"YYYY":return s(n.year);case"Z":var v="+",D=Math.round(n.timezone/60),p=n.timezone%60;p<0&&(p*=-1),D<0&&(v="-",D*=-1);var g=v+h(D,2)+":"+h(p,2);return s(g);case"ZZ":var w="+",k=Math.round(n.timezone/60),b=n.timezone%60;b<0&&(b*=-1),k<0&&(w="-",k*=-1);var M=w+h(k,2)+""+h(b,2);return s(M);case"X":return a.unix();case"LT":return a.format("h:m a");case"L":return a.format("YYYY/MM/DD");case"l":return a.format("YYYY/M/D");case"LL":return a.format("MMMM DD YYYY");case"ll":return a.format("MMM DD YYYY");case"LLL":return a.format("MMMM YYYY DD h:m a");case"lll":return a.format("MMM YYYY DD h:m a");case"LLLL":return a.format("dddd D MMMM YYYY h:m a");case"llll":return a.format("ddd D MMM YYYY h:m a")}}var a=this,r=/([[^[]*])|(\\)?(Mo|MM?M?M?|Do|DD?D?D?|ddddd|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|X|LT|ll?l?l?|LL?L?L?)/g,n={year:a.year(),month:a.month(),hour:a.hours(),minute:a.minutes(),second:a.seconds(),date:a.date(),timezone:a.zone(),unix:a.unix()},i=a.formatNumber(),s=function(e){return i?u(e):e};return e?e.replace(r,t):"YYYY-MM-DD HH:mm:ss a".replace(r,t)}},{key:"diff",value:function(e,t,a){var r=this,n=e,i=r.gDate-n.gDate-0,s=r.year()-n.year(),o=r.month()-n.month(),u=(r.date()-n.date())*-1,h=void 0;return h="months"===t||"month"===t?12*s+o+u/30:"years"===t||"year"===t?s+(o+u/30)/12:"seconds"===t||"second"===t?i/1e3:"minutes"===t||"minute"===t?i/6e4:"hours"===t||"hour"===t?i/36e5:"days"===t||"day"===t?i/864e5:"weeks"===t||"week"===t?i/6048e5:i,h<0&&(h*=-1),a?h:Math.round(h)}},{key:"startOf",value:function(e){switch(e){case"years":case"year":return new t([this.year(),1,1]);case"months":case"month":return new t([this.year(),this.month(),1]);case"days":case"day":return new t([this.year(),this.month(),this.date(),0,0,0]);case"hours":case"hour":return new t([this.year(),this.month(),this.date(),this.hours(),0,0]);case"minutes":case"minute":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),0]);case"seconds":case"second":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),this.seconds()]);case"weeks":case"week":var a=this.pDate.weekDayNumber;return 0===a?new t([this.year(),this.month(),this.date()]):new t([this.year(),this.month(),this.date()]).subtract("days",a);default:return this}}},{key:"endOf",value:function(e){switch(e){case"years":case"year":var a=this.isLeapYear()?30:29;return new t([this.year(),12,a,23,59,59]);case"months":case"month":var r=this.daysInMonth(this.year(),this.month());return new t([this.year(),this.month(),r,23,59,59]);case"days":case"day":return new t([this.year(),this.month(),this.date(),23,59,59]);case"hours":case"hour":return new t([this.year(),this.month(),this.date(),this.hours(),59,59]);case"minutes":case"minute":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),59]);case"seconds":case"second":return new t([this.year(),this.month(),this.date(),this.hours(),this.minutes(),this.seconds()]);case"weeks":case"week":var n=this.pDate.weekDayNumber;return n=6===n?7:6-n,new t([this.year(),this.month(),this.date()]).add("days",n);default:return this}}},{key:"sod",value:function(){return this.startOf("day")}},{key:"eod",value:function(){return this.endOf("day")}},{key:"zone",value:function(){return this.pDate.timeZoneOffset}},{key:"local",value:function(){var e=void 0;if(this._utcMode){var t=60*this.pDate.timeZoneOffset*1e3;return e=this.pDate.timeZoneOffset<0?this.valueOf()-t:this.valueOf()+t,this.gDate=new Date(e),this._updatePDate(),this._utcMode=!1,this}return this}},{key:"utc",value:function(e){var a=void 0;if(e)return new t(e).utc();if(this._utcMode)return this;var r=60*this.pDate.timeZoneOffset*1e3;return a=this.pDate.timeZoneOffset<0?this.valueOf()+r:this.valueOf()-r,this.gDate=new Date(a),this._updatePDate(),this._utcMode=!0,this}},{key:"isUtc",value:function(){return this._utcMode}},{key:"isDST",value:function(){var e=this.month(),t=this.date();return!(e<7)&&(7==e&&t>=2||e>=7||void 0)}},{key:"isLeapYear",value:function(){return this.algorithms.isLeapPersian(this.year())}},{key:"daysInMonth",value:function(e,t){var a=e||this.year(),r=t||this.month();return r<1||r>12?0:r<7?31:r<12?30:this.algorithms.isLeapPersian(a)?30:29}},{key:"toDate",value:function(){return this.gDate}},{key:"toArray",value:function(){return[this.year(),this.month(),this.date(),this.hour(),this.minute(),this.second(),this.millisecond()]}},{key:"_valueOf",value:function(){return this.gDate.valueOf()}},{key:"unix",value:function(e){var a=void 0;if(e)return new t(1e3*e);var r=this.gDate.valueOf().toString();return a=r.substring(0,r.length-3),parseInt(a)}},{key:"isPersianDate",value:function(e){return e instanceof t}},{key:"millisecond",value:function(e){return this.milliseconds(e)}},{key:"milliseconds",value:function(e){return e?(this.gDate.setMilliseconds(e),this._updatePDate(),this):this.pDate.milliseconds}},{key:"second",value:function(e){return this.seconds(e)}},{key:"seconds",value:function(e){return e|0===e?(this.gDate.setSeconds(e),this._updatePDate(),this):this.pDate.seconds}},{key:"minute",value:function(e){return this.minutes(e)}},{key:"minutes",value:function(e){return e||0===e?(this.gDate.setMinutes(e),this._updatePDate(),this):parseInt(this.pDate.minutes)}},{key:"hour",value:function(e){return this.hours(e)}},{key:"hours",value:function(e){return e|0===e?(this.gDate.setHours(e),this._updatePDate(),this):this.pDate.hours}},{key:"dates",value:function(e){return this.date(e)}},{key:"date",value:function(e){if(e||0===e){var t=this.algorithms.getPersianArrayFromPDate(this.pDate);return t[2]=e,this.gDate=this.algorithms.persianArrayToGregorianDate(t),this._updatePDate(),this}return this.pDate.date}},{key:"days",value:function(){return this.day()}},{key:"day",value:function(){return this.pDate.day}},{key:"month",value:function(e){if(e|0===e){var t=this.algorithms.getPersianArrayFromPDate(this.pDate);return t[1]=e,this.gDate=this.algorithms.persianArrayToGregorianDate(t),this._updatePDate(),this}return this.pDate.month}},{key:"years",value:function(e){return this.year(e)}},{key:"year",value:function(e){if(e|0===e){var t=this.algorithms.getPersianArrayFromPDate(this.pDate);return t[0]=e,this.gDate=this.algorithms.persianArrayToGregorianDate(t),this._updatePDate(),this}return this.pDate.year}},{key:"getFirstWeekDayOfMonth",value:function(e,t){var a=this.algorithms.calcPersian(e,t,1),r=this.algorithms.calcGregorian(a[0],a[1],a[2]);return r[3]+2===8?1:r[3]+2===7?7:r[3]+2}},{key:"clone",value:function(){return new t(this.gDate)}},{key:"_updatePDate",value:function(){this.pDate=this.algorithms.toPersianDate(this.gDate)}},{key:"valueOf",value:function(){return this._valueOf()}}],[{key:"_utc",value:function(e){return e?new t(e).utc():(new t).utc()}},{key:"_unix",value:function(e){return e?new t(1e3*e).unix():(new t).unix()}}]),t}();e.exports=l},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var a=0;a0?474:473))%2820+474+38)%2816<682}},{key:"gregorianToJd",value:function(e,t,a){return i-1+365*(e-1)+Math.floor((e-1)/4)+-Math.floor((e-1)/100)+Math.floor((e-1)/400)+Math.floor((367*t-362)/12+(t<=2?0:this.isLeapGregorian(e)?-1:-2)+a)}},{key:"jdToGregorian",value:function(e){var t=Math.floor(e-.5)+.5,a=t-i,r=Math.floor(a/146097),n=u(a,146097),s=Math.floor(n/36524),o=u(n,36524),h=Math.floor(o/1461),c=u(o,1461),f=Math.floor(c/365),d=400*r+100*s+4*h+f;4!=s&&4!=f&&d++;var l=t-this.gregorianToJd(d,1,1),m=t=0?474:473),n=474+u(r,2820),a+(t<=7?31*(t-1):30*(t-1)+6)+Math.floor((682*n-110)/2816)+365*(n-1)+1029983*Math.floor(r/2820)+(s-1)}},{key:"jdToPersian",value:function(e){var t=void 0,a=void 0,r=void 0,n=void 0,i=void 0,s=void 0,o=void 0,h=void 0,c=void 0,f=void 0;return e=Math.floor(e)+.5,n=e-this.persianToJd(475,1,1),i=Math.floor(n/1029983),s=u(n,1029983),1029982===s?o=2820:(h=Math.floor(s/366),c=u(s,366),o=Math.floor((2134*h+2816*c+2815)/1028522)+h+1),t=o+2820*i+474,t<=0&&(t-=1),f=e-this.persianToJd(t,1,1)+1,a=f<=186?Math.ceil(f/31):Math.ceil((f-6)/30),r=e-this.persianToJd(t,a,1)+1,new Array(t,a,r)}},{key:"calcPersian",value:function(e,t,a){var r=this.persianToJd(e,t,a),n=this.jdToGregorian(r);return new Array(n[0],n[1]-1,n[2])}},{key:"calcGregorian",value:function(e,t,a){var r=this.gregorianToJd(e,t+1,a)+Math.floor(.5)/86400,n=this.jdToPersian(r),i=this.jwday(r);return new Array(n[0],n[1],n[2],i)}},{key:"toPersianDate",value:function(e){var t=this.calcGregorian(e.getFullYear(),e.getMonth(),e.getDate()),a={};return a.monthDayNumber=t[2]-1,6==t[3]?a.weekDayNumber=1:5===t[3]?a.weekDayNumber=0:4===t[3]?a.weekDayNumber=6:3===t[3]?a.weekDayNumber=5:2===t[3]?a.weekDayNumber=4:1===t[3]?a.weekDayNumber=3:0===t[3]&&(a.weekDayNumber=2),a.year=t[0],a.month=t[1],a.day=a.weekDayNumber,a.date=t[2],a.hours=e.getHours(),a.minutes=e.getMinutes()<10?"0"+e.getMinutes():e.getMinutes(),a.seconds=e.getSeconds(),a.milliseconds=e.getMilliseconds(),a.timeZoneOffset=e.getTimezoneOffset(),a}},{key:"persianArrayToGregorianDate",value:function(e){void 0===e[1]&&(e[1]=1),void 0===e[2]&&(e[2]=1);var t=this.calcPersian(e[0],e[1],e[2]),a=new Date;return a.setYear(t[0]),a.setMonth(t[1]),a.setDate(t[2]),a.setHours(e[3]?e[3]:0),a.setMinutes(e[4]?e[4]:0),a.setSeconds(e[5]?e[5]:0),a.setMilliseconds(e[6]?e[6]:0),a}},{key:"getPersianArrayFromPDate",value:function(e){return[e.year,e.month,e.date,e.hours,e.minutes,e.seconds,e.milliseconds]}}]),e}();e.exports=h},function(e,t,a){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=a(1),i=(new n).normalizeDuration,s=(new n).absRound,o=function e(t,a){r(this,e);var n={},o=this._data={},u=0,h=i(t,a);n[h.unit]=h.value,u=n.milliseconds||n.millisecond||n.ms||0;var c=n.years||n.year||n.y||0,f=n.months||n.month||n.M||0,d=n.weeks||n.w||n.week||0,l=n.days||n.d||n.day||0,m=n.hours||n.hour||n.h||0,y=n.minutes||n.minute||n.m||0,v=n.seconds||n.second||n.s||0;return this._milliseconds=u+1e3*v+6e4*y+36e5*m,this._days=l+7*d,this._months=f+12*c,o.milliseconds=u%1e3,v+=u/1e3,o.seconds=v%60,y+=s(v/60),o.minutes=y%60,m+=s(y/60),o.hours=m%24,l+=s(m/24),l+=7*d,o.days=l%30,f+=s(l/30),o.months=f%12,c+=s(f/12),o.years=c,this};o.prototype={valueOf:function(){return this._milliseconds+864e5*this._days+2592e6*this._months}},e.exports=o},function(e,t,a){"use strict";var r=a(2);String.prototype.toPersianDigit=function(e){return this.replace(/\d+/g,function(t){var a=[],r=[],n=void 0,i=void 0;for(n=0;n", "description": "Javascript date library for parsing, validating, manipulating, and formatting persian dates System.", diff --git a/src/algorithms.js b/src/algorithms.js index 01b5f1b..e543628 100644 --- a/src/algorithms.js +++ b/src/algorithms.js @@ -12,7 +12,7 @@ class Algorithms { * @param j * @returns {*} */ - jwday(j) { + jwday (j) { let mod = function (a, b) { return a - (b * Math.floor(a / b)); }; @@ -25,7 +25,7 @@ class Algorithms { * @param year * @returns {boolean} */ - isLeapGregorian(year) { + isLeapGregorian (year) { return ((year % 4) === 0) && (!(((year % 100) === 0) && ((year % 400) !== 0))); } @@ -34,8 +34,10 @@ class Algorithms { * @param year * @returns {boolean} */ - isLeapPersian(year) { + isLeapPersian (year) { return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682; + // https://fa.wikipedia.org/wiki/%DA%AF%D8%A7%D9%87%E2%80%8C%D8%B4%D9%85%D8%A7%D8%B1%DB%8C_%D9%87%D8%AC%D8%B1%DB%8C_%D8%AE%D9%88%D8%B1%D8%B4%DB%8C%D8%AF%DB%8C_%D8%AD%D8%B3%D8%A7%D8%A8%DB%8C + // return parseFloat('0.' + ((year + 2346) * (0.24219858156)).toString().split('.')[1]) < 0.24219858156; } @@ -46,9 +48,9 @@ class Algorithms { * @param day * @returns {number} */ - gregorianToJd(year, month, day) { + gregorianToJd (year, month, day) { return (GREGORIAN_EPOCH - 1) + (365 * (year - 1)) + Math.floor((year - 1) / 4) + (-Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12) + ((month <= 2) ? 0 : (this.isLeapGregorian(year) ? -1 : -2) - ) + day); + ) + day); } @@ -57,25 +59,25 @@ class Algorithms { * @param jd * @returns {Array} */ - jdToGregorian(jd) { + jdToGregorian (jd) { //let wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, dyindex, year, yearday, leapadj; let wjd = Math.floor(jd - 0.5) + 0.5, - depoch = wjd - GREGORIAN_EPOCH, - quadricent = Math.floor(depoch / 146097), - dqc = mod(depoch, 146097), - cent = Math.floor(dqc / 36524), - dcent = mod(dqc, 36524), - quad = Math.floor(dcent / 1461), - dquad = mod(dcent, 1461), - yindex = Math.floor(dquad / 365), - year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex; + depoch = wjd - GREGORIAN_EPOCH, + quadricent = Math.floor(depoch / 146097), + dqc = mod(depoch, 146097), + cent = Math.floor(dqc / 36524), + dcent = mod(dqc, 36524), + quad = Math.floor(dcent / 1461), + dquad = mod(dcent, 1461), + yindex = Math.floor(dquad / 365), + year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex; if (!((cent == 4) || (yindex == 4))) { year++; } let yearday = wjd - this.gregorianToJd(year, 1, 1), - leapadj = ((wjd < this.gregorianToJd(year, 3, 1)) ? 0 : (this.isLeapGregorian(year) ? 1 : 2)), - month = Math.floor((((yearday + leapadj) * 12) + 373) / 367), - day = (wjd - this.gregorianToJd(year, month, 1)) + 1; + leapadj = ((wjd < this.gregorianToJd(year, 3, 1)) ? 0 : (this.isLeapGregorian(year) ? 1 : 2)), + month = Math.floor((((yearday + leapadj) * 12) + 373) / 367), + day = (wjd - this.gregorianToJd(year, month, 1)) + 1; return new Array(year, month, day); } @@ -87,12 +89,12 @@ class Algorithms { * @param day * @returns {*} */ - persianToJd(year, month, day) { + persianToJd (year, month, day) { let epbase, epyear; epbase = year - ((year >= 0) ? 474 : 473); epyear = 474 + mod(epbase, 2820); return day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6) - ) + Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 + Math.floor(epbase / 2820) * 1029983 + (PERSIAN_EPOCH - 1); + ) + Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 + Math.floor(epbase / 2820) * 1029983 + (PERSIAN_EPOCH - 1); } @@ -101,7 +103,7 @@ class Algorithms { * @param jd * @returns {Array} */ - jdToPersian(jd) { + jdToPersian (jd) { let year, month, day, depoch, cycle, cyear, ycycle, aux1, aux2, yday; jd = Math.floor(jd) + 0.5; depoch = jd - this.persianToJd(475, 1, 1); @@ -133,9 +135,9 @@ class Algorithms { * @param day * @returns {Array} */ - calcPersian(year, month, day) { + calcPersian (year, month, day) { let j = this.persianToJd(year, month, day), - date = this.jdToGregorian(j); + date = this.jdToGregorian(j); return new Array(date[0], date[1] - 1, date[2]); } @@ -147,11 +149,11 @@ class Algorithms { * @param day * @returns {Array} */ - calcGregorian(year, month, day) { + calcGregorian (year, month, day) { // Update Julian day let j = this.gregorianToJd(year, month + 1, day) + (Math.floor(0 + 60 * (0 + 60 * 0) + 0.5) / 86400.0), - // Update Persian Calendar - perscal = this.jdToPersian(j), weekday = this.jwday(j); + // Update Persian Calendar + perscal = this.jdToPersian(j), weekday = this.jwday(j); return new Array(perscal[0], perscal[1], perscal[2], weekday); } @@ -161,9 +163,9 @@ class Algorithms { * @param gd * @returns {{}} */ - toPersianDate(gd) { + toPersianDate (gd) { let pa = this.calcGregorian(gd.getFullYear(), gd.getMonth(), gd.getDate()), - output = {}; + output = {}; output.monthDayNumber = pa[2] - 1; if (pa[3] == 6) { output.weekDayNumber = 1; @@ -198,10 +200,16 @@ class Algorithms { * @param parray persian-date array * @returns {Date} */ - persianArrayToGregorianDate(parray) { + persianArrayToGregorianDate (parray) { + if (parray[1] === undefined) { + parray[1] = 1; + } + if (parray[2] === undefined) { + parray[2] = 1; + } // Howha : javascript Cant Parse this array truly 2011,2,20 - let pd = this.calcPersian(parray[0] ? parray[0] : 0, parray[1] ? parray[1] : 1, parray[2] ? parray[2] : 1), - gDate = new Date(pd[0], pd[1], pd[2]); + let pd = this.calcPersian(parray[0], parray[1], parray[2]), + gDate = new Date(); gDate.setYear(pd[0]); gDate.setMonth(pd[1]); gDate.setDate(pd[2]); @@ -218,7 +226,7 @@ class Algorithms { * @param pDate * @returns {array} */ - getPersianArrayFromPDate(pDate) { + getPersianArrayFromPDate (pDate) { return [pDate.year, pDate.month, pDate.date, pDate.hours, pDate.minutes, pDate.seconds, pDate.milliseconds]; } } diff --git a/test/test.js b/test/test.js index 3c9b663..6819299 100644 --- a/test/test.js +++ b/test/test.js @@ -894,7 +894,7 @@ describe('Subtract', function () { assert.deepEqual(a, [1390, 1, 1, 1, 1, 1, 0]); }); it('Month', function () { - let a = new pDate([1391, 2, 1, 1, 1, 1]).subtract('month', 2).toArray(); + let a = new pDate([1391, 2, 1, 1, 1, 1]).subtract('month', 1).toArray(); assert.deepEqual(a, [1391, 1, 1, 1, 1, 1, 0]); }); it('Days', function () {