Date.js 7.4 KB

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