You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.0 KiB

  1. /**
  2. * @author Javad Mowlanezhad jmowla@gmail.com
  3. * @version 1.0.0
  4. */
  5. (function($) {
  6. function JalaliCalendar() {
  7. var _jDIM = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29];
  8. var _gDIM = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  9. /**
  10. * Checks input parameter for Array object, if the object is Array returns true otherwise false
  11. * @param a
  12. */
  13. function _isArray(a) {
  14. return (a && a.constructor && a.constructor.toString().match(/\Array\(\)/));
  15. }
  16. function _div(a, b) {
  17. return Math.floor(a / b);
  18. }
  19. function _ds2da(ds) {
  20. if (_isArray(ds))
  21. return ds;
  22. if (ds == "")
  23. return [];
  24. var da = ds.split(" ")[0].split("-");
  25. if (da[1] == undefined)
  26. da = ds.split(" ")[0].split("/");
  27. return da;
  28. }
  29. function _ivda(da) {
  30. return !(da[0] === "" || da[1] === "" || da[2] === "");
  31. }
  32. function _isjvd(d) {
  33. d = _ds2da(d);
  34. var j = _g2j(_j2g(d));
  35. return d[0] === j[0] && d[1] === j[1] && d[2] === j[2];
  36. }
  37. function _isgvd(d) {
  38. d = _ds2da(d);
  39. var g = _j2g(_g2j(d));
  40. return d[0] === g[0] && d[1] === g[1] && d[2] === g[2];
  41. }
  42. function _j2JUL(j) {
  43. var jy, jm, jd, jdn,i;
  44. jy = j[0] - 979;
  45. jm = j[1] - 1;
  46. jd = j[2] - 1;
  47. jdn = 365 * jy + _div(jy, 33) * 8 + _div((jy % 33 + 3), 4);
  48. for (i = 0; i < jm; ++i)
  49. jdn += _jDIM[i];
  50. return jdn + jd;
  51. }
  52. function _JUL2j(gdn) {
  53. var gy, gm, gd, leap, i;
  54. gy = 1600 + 400 * _div(gdn, 146097);
  55. gdn = gdn % 146097;
  56. leap = 1;
  57. if (gdn >= 36525) {
  58. gdn--;
  59. gy += 100 * _div(gdn, 36524);
  60. gdn = gdn % 36524;
  61. if (gdn >= 365)
  62. gdn++;
  63. else
  64. leap = 0;
  65. }
  66. gy += 4 * _div(gdn, 1461);
  67. gdn %= 1461;
  68. if (gdn >= 366) {
  69. leap = 0;
  70. gdn--;
  71. gy += _div(gdn, 365);
  72. gdn = gdn % 365;
  73. }
  74. for (i = 0; gdn >= _gDIM[i] + (i == 1 && leap); i++)
  75. gdn -= _gDIM[i] + (i == 1 && leap);
  76. gm = i + 1;
  77. gd = gdn + 1;
  78. return [gy, gm, gd];
  79. }
  80. function _g2JUL(g) {
  81. var gy, gm, gd, gdn, i;
  82. gy = g[0] - 1600;
  83. gm = g[1] - 1;
  84. gd = g[2] - 1;
  85. gdn = 365 * gy + _div((gy + 3), 4) - _div((gy + 99), 100) + _div((gy + 399), 400);
  86. for (i = 0; i < gm; ++i)
  87. gdn += _gDIM[i];
  88. if (gm > 1 && ((gy % 4 == 0 && gy % 100 != 0) || (gy % 400 == 0)))
  89. ++gdn;
  90. return gdn + gd;
  91. }
  92. function _JUL2g(jdn) {
  93. var j_np, jy, jm, jd, i;
  94. j_np = _div(jdn, 12053);
  95. jdn %= 12053;
  96. jy = 979 + 33 * j_np + 4 * _div(jdn, 1461);
  97. jdn %= 1461;
  98. if (jdn >= 366) {
  99. jy += _div((jdn - 1), 365);
  100. jdn = (jdn - 1) % 365;
  101. }
  102. for (i = 0; i < 11 && jdn >= _jDIM[i]; ++i)
  103. jdn -= _jDIM[i];
  104. jm = i + 1;
  105. jd = jdn + 1;
  106. return [jy, jm, jd];
  107. }
  108. function _j2g(j) {
  109. j = _ds2da(j);
  110. if (!_ivda(j))
  111. return [];
  112. return _JUL2j(_j2JUL(j) + 79);
  113. }
  114. function _g2j(g) {
  115. g = _ds2da(g);
  116. if (!_ivda(g))
  117. return [];
  118. return _JUL2g(_g2JUL(g) - 79);
  119. }
  120. return {
  121. gregorianToJalali: function(g) {
  122. return _g2j(g);
  123. },
  124. gregorianToJalaliStr: function(_g) {
  125. var g = _ds2da(_g);
  126. if (!_ivda(g))
  127. return "";
  128. var j = _g2j(g);
  129. return j[0] + "/" + (j[1] > 9 ? j[1] : "0" + j[1]) + "/" + (j[2] > 9 ? j[2] : "0" + j[2]);
  130. },
  131. jalaliToGregorianStr: function (_j, _st) {
  132. var j = _ds2da(_j);
  133. if (!_ivda(j))
  134. return "";
  135. var g = _j2g(j);
  136. return g[0] + "/" + g[1] + "/" + g[2] + (!_st ? "" : " " + _j.split(" ")[1].split(".")[0]);
  137. },
  138. jalaliToGregorian: function(j) {
  139. return _j2g(j);
  140. },
  141. today: function() {
  142. var _today = new Date();
  143. return this.gregorianToJalaliStr([_today.getFullYear(),(_today.getMonth() + 1),_today.getDate()]);
  144. },
  145. isValidJ: function(d) {
  146. return _isjvd(d);
  147. },
  148. isValidG: function(d) {
  149. return _isgvd(d);
  150. },
  151. getDaysInMonth: function(y, m) {
  152. return 32 - new Date(y, m, 32).getDate();
  153. },
  154. version: "1.0.0"
  155. };
  156. };
  157. if ($._gDate == undefined) {
  158. Date.prototype._getFullYear = Date.prototype.getFullYear;
  159. Date.prototype._setFullYear = Date.prototype.setFullYear;
  160. Date.prototype._getMonth = Date.prototype.getMonth;
  161. Date.prototype._setMonth = Date.prototype.setMonth;
  162. Date.prototype._getDate = Date.prototype.getDate;
  163. Date.prototype._setDate = Date.prototype.setDate;
  164. Date.prototype._getCJD = function() {
  165. return $.jalaliCalendar.gregorianToJalali([this._getFullYear(), this._getMonth() + 1, this._getDate()]);
  166. };
  167. Date.prototype.getFullYear = function() {
  168. return this._getCJD()[0];
  169. };
  170. Date.prototype.setFullYear = function(y) {
  171. var j = this._getCJD();
  172. var g = $.jalaliCalendar.jalaliToGregorian([y, j[1], j[2]]);
  173. this._setDate(1);
  174. this._setFullYear(g[0]);
  175. this._setMonth(g[1] - 1);
  176. this._setDate(g[2]);
  177. };
  178. Date.prototype.getMonth = function() {
  179. return this._getCJD()[1] - 1;
  180. };
  181. Date.prototype.setMonth = function(m) {
  182. var j = this._getCJD();
  183. var g = $.jalaliCalendar.jalaliToGregorian([j[0], m + 1, j[2]]);
  184. this._setDate(1);
  185. this._setFullYear(g[0]);
  186. this._setMonth(g[1] - 1);
  187. this._setDate(g[2]);
  188. };
  189. Date.prototype.getDate = function() {
  190. return this._getCJD()[2];
  191. };
  192. Date.prototype.setDate = function(d) {
  193. var j = this._getCJD();
  194. var g = $.jalaliCalendar.jalaliToGregorian([j[0], j[1], d]);
  195. this._setDate(1);
  196. this._setFullYear(g[0]);
  197. this._setMonth(g[1] - 1);
  198. this._setDate(g[2]);
  199. };
  200. $._gDate = Date; // save original Date object for further reference and use
  201. window.Date = function() {
  202. this.prototype = $._gDate.prototype;
  203. var _p = [], _al = arguments.length;
  204. for (var i = 0; i < _al; i++)
  205. _p.push(arguments[i]);
  206. var _d = null;
  207. if (_al == 0)
  208. _d = new $._gDate();
  209. else if (_al == 1) {
  210. _d = new $._gDate(_p[0]);
  211. } else if (_al >= 3) {
  212. _p[1]++;
  213. var _g = $.jalaliCalendar.jalaliToGregorian(_p);
  214. for (i = 3; i < _al; i++)
  215. _g.push(_p[i]);
  216. for (i = _al; i < 7; i++)
  217. _g.push(0);
  218. _d = new $._gDate(_g[0], _g[1] - 1, _g[2], _g[3], _g[4], _g[5], _g[6]);
  219. }
  220. return _d;
  221. };
  222. }
  223. if ($.jalaliCalendar == undefined) {
  224. $.jalaliCalendar = new JalaliCalendar();
  225. window.jalaliCalendar= $.jalaliCalendar;
  226. }
  227. })(jQuery);