Date.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.date.buddhist.Date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.buddhist.Date"] = true;
  8. dojo.provide("dojox.date.buddhist.Date");
  9. dojo.experimental("dojox.date.buddhist.Date");
  10. dojo.declare("dojox.date.buddhist.Date", null, {
  11. _date: 0,
  12. _month: 0,
  13. _year: 0,
  14. _hours: 0,
  15. _minutes: 0,
  16. _seconds: 0,
  17. _milliseconds: 0,
  18. _day: 0,
  19. constructor: function(){
  20. // summary: This is the constructor
  21. // description:
  22. // This fucntion initialize the date object values
  23. //
  24. // example:
  25. // | var date1 = new dojox.date.buddhist.Date();
  26. // |
  27. // | var date2 = new dojox.date.buddhist.Date(date1);
  28. // |
  29. // | var date3 = new dojox.date.buddhist.Date(2552,2,12);
  30. var len = arguments.length;
  31. if(!len){// use the current date value, added "" to the similarity to date
  32. this.fromGregorian(new Date());
  33. }else if(len == 1){
  34. var arg0 = arguments[0];
  35. if(typeof arg0 == "number"){ // this is time "valueof"
  36. arg0 = new Date(arg0);
  37. }
  38. if(arg0 instanceof Date){
  39. this.fromGregorian(arg0);
  40. }else if(arg0 == ""){
  41. this._date = new Date("");
  42. }else{
  43. this._year = arg0._year;
  44. this._month = arg0._month;
  45. this._date = arg0._date;
  46. this._hours = arg0._hours;
  47. this._minutes = arg0._minutes;
  48. this._seconds = arg0._seconds;
  49. this._milliseconds = arg0._milliseconds;
  50. }
  51. }else if(len >=3){
  52. this._year += arguments[0];
  53. this._month += arguments[1];
  54. this._date += arguments[2];
  55. if(this._month >11){
  56. console.warn("the month is incorrect , set 0");
  57. this._month = 0;
  58. }
  59. this._hours += arguments[3] || 0;
  60. this._minutes += arguments[4] || 0;
  61. this._seconds += arguments[5] || 0;
  62. this._milliseconds += arguments[6] || 0;
  63. }
  64. },
  65. getDate: function(/*boolean?*/isNumber){
  66. // summary: This function returns the date value (0 - 30)
  67. //
  68. // example:
  69. // | var date1 = new dojox.date.buddhist.Date();
  70. // |
  71. // | console.log(date1.getDate());
  72. return parseInt(this._date);
  73. },
  74. getMonth: function(){
  75. // summary: This function return the month value ( 0 - 11 )
  76. //
  77. // example:
  78. // | var date1 = new dojox.date.buddhist.Date();
  79. // |
  80. // | console.log(date1.getMonth()+1);
  81. return parseInt(this._month);
  82. },
  83. getFullYear: function(){
  84. // summary: This function return the Year value
  85. //
  86. // example:
  87. // | var date1 = new dojox.date.buddhist.Date();
  88. // |
  89. // | console.log(date1.getFullYear());
  90. return parseInt(this._year);
  91. },
  92. getHours: function(){
  93. //summary: returns the Hour value
  94. return this._hours;
  95. },
  96. getMinutes: function(){
  97. //summary: returns the Minuites value
  98. return this._minutes;
  99. },
  100. getSeconds: function(){
  101. //summary: returns the seconde value
  102. return this._seconds;
  103. },
  104. getMilliseconds: function(){
  105. //summary: returns the Milliseconds value
  106. return this._milliseconds;
  107. },
  108. setDate: function(/*number*/date){
  109. // summary: This function sets the Date
  110. // example:
  111. // | var date1 = new dojox.date.buddhist.Date();
  112. // | date1.setDate(2);
  113. date = parseInt(date);
  114. if(date > 0 && date <= this._getDaysInMonth(this._month, this._year)){
  115. this._date = date;
  116. }else{
  117. var mdays;
  118. if(date>0){
  119. for(mdays = this._getDaysInMonth(this._month, this._year);
  120. date > mdays;
  121. date -= mdays,mdays = this._getDaysInMonth(this._month, this._year)){
  122. this._month++;
  123. if(this._month >= 12){this._year++; this._month -= 12;}
  124. }
  125. this._date = date;
  126. }else{
  127. for(mdays = this._getDaysInMonth((this._month-1)>=0 ?(this._month-1) :11 ,((this._month-1)>=0)? this._year: this._year-1);
  128. date <= 0;
  129. mdays = this._getDaysInMonth((this._month-1)>=0 ? (this._month-1) :11,((this._month-1)>=0)? this._year: this._year-1)){
  130. this._month--;
  131. if(this._month < 0){this._year--; this._month += 12;}
  132. date+=mdays;
  133. }
  134. this._date = date;
  135. }
  136. }
  137. return this;
  138. },
  139. setFullYear: function(/*number*/year, /*number?*/month, /*number?*/ date){
  140. // summary: This function set Year
  141. //
  142. // example:
  143. // | var date1 = new dojox.date.buddhist.Date();
  144. // | date1.setFullYear(2552);
  145. // | date1.setFullYear(2552, 1, 1);
  146. this._year = parseInt(year);
  147. },
  148. setMonth: function(/*number*/month){
  149. // summary: This function set Month
  150. //
  151. // example:
  152. // | var date1 = new dojox.date.buddhist.Date();
  153. // | date1.setMonth(0); //first month
  154. this._year += Math.floor(month / 12);
  155. this._month = Math.floor(month % 12);
  156. for(; this._month < 0; this._month = this._month+12);
  157. },
  158. setHours: function(){
  159. //summary: set the Hours 0-23
  160. var hours_arg_no = arguments.length;
  161. var hours = 0;
  162. if(hours_arg_no >= 1){
  163. hours = parseInt(arguments[0]);
  164. }
  165. if(hours_arg_no >= 2){
  166. this._minutes = parseInt(arguments[1]);
  167. }
  168. if(hours_arg_no >= 3){
  169. this._seconds = parseInt(arguments[2]);
  170. }
  171. if(hours_arg_no == 4){
  172. this._milliseconds = parseInt(arguments[3]);
  173. }
  174. while(hours >= 24){
  175. this._date++;
  176. var mdays = this._getDaysInMonth(this._month, this._year);
  177. if(this._date > mdays){
  178. this._month ++;
  179. if(this._month >= 12){this._year++; this._month -= 12;}
  180. this._date -= mdays;
  181. }
  182. hours -= 24;
  183. }
  184. this._hours = hours;
  185. },
  186. setMinutes: function(/*number*/minutes){
  187. //summary: set the Minutes frm 0-59
  188. while(minutes >= 60){
  189. this._hours++;
  190. if(this._hours >= 24){
  191. this._date++;
  192. this._hours -= 24;
  193. var mdays = this._getDaysInMonth(this._month, this._year);
  194. if(this._date > mdays){
  195. this._month ++;
  196. if(this._month >= 12){this._year++; this._month -= 12;}
  197. this._date -= mdays;
  198. }
  199. }
  200. minutes -= 60;
  201. }
  202. this._minutes = minutes;
  203. },
  204. setSeconds: function(/*number*/seconds){
  205. //summary: set the Seconds from 0-59
  206. while(seconds >= 60){
  207. this._minutes++;
  208. if(this._minutes >= 60){
  209. this._hours++;
  210. this._minutes -= 60;
  211. if(this._hours >= 24){
  212. this._date++;
  213. this._hours -= 24;
  214. var mdays = this._getDaysInMonth(this._month, this._year);
  215. if(this._date > mdays){
  216. this._month ++;
  217. if(this._month >= 12){this._year++; this._month -= 12;}
  218. this._date -= mdays;
  219. }
  220. }
  221. }
  222. seconds -= 60;
  223. }
  224. this._seconds = seconds;
  225. },
  226. setMilliseconds: function(/*number*/milliseconds){
  227. //summary: set the milliseconds
  228. while(milliseconds >= 1000){
  229. this.setSeconds++;
  230. if(this.setSeconds >= 60){
  231. this._minutes++;
  232. this.setSeconds -= 60;
  233. if(this._minutes >= 60){
  234. this._hours++;
  235. this._minutes -= 60;
  236. if(this._hours >= 24){
  237. this._date++;
  238. this._hours -= 24;
  239. var mdays = this._getDaysInMonth(this._month, this._year);
  240. if(this._date > mdays){
  241. this._month ++;
  242. if(this._month >= 12){this._year++; this._month -= 12;}
  243. this._date -= mdays;
  244. }
  245. }
  246. }
  247. }
  248. milliseconds -= 1000;
  249. }
  250. this._milliseconds = milliseconds;
  251. },
  252. toString: function(){
  253. // summary: This returns a string representation of the date in "dd, MM, YYYY HH:MM:SS" format
  254. return this._date + ", " + this._month + ", " + this._year + " " + this._hours + ":" + this._minutes + ":" + this._seconds; // String
  255. },
  256. //FIXME: remove this and replace usage with dojox.date.buddhist.getDaysInMonth?
  257. _getDaysInMonth: function(/*number*/month, /*number*/ year){
  258. return dojo.date.getDaysInMonth(new Date(year-543, month));
  259. },
  260. fromGregorian: function(/*Date*/gdate){
  261. // summary: This function sets this Date to the Hebrew Date corresponding to the Gregorian Date
  262. var date = new Date(gdate);
  263. this._date = date.getDate();
  264. this._month = date.getMonth();
  265. this._year = date.getFullYear()+543;
  266. this._hours = date.getHours();
  267. this._minutes = date.getMinutes();
  268. this._seconds = date.getSeconds();
  269. this._milliseconds = date.getMilliseconds();
  270. this._day = date.getDay();
  271. return this;
  272. },
  273. toGregorian: function(){
  274. // summary: This returns the equivalent Gregorian date value as a Date object
  275. return new Date(this._year-543, this._month, this._date, this._hours, this._minutes, this._seconds, this._milliseconds); // Date
  276. },
  277. getDay: function(){
  278. // summary: This function return Week Day value ( 0 - 6 )
  279. return this.toGregorian().getDay(); // int
  280. }
  281. });
  282. dojox.date.buddhist.Date.prototype.valueOf = function(){
  283. return this.toGregorian().valueOf();
  284. };
  285. }