PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ZofPlus/web/js/date.js

https://gitlab.com/BGCX261/zofplus-svn-to-git
JavaScript | 505 lines | 148 code | 36 blank | 321 comment | 23 complexity | e3800226415581fdacf650f4a5060c04 MD5 | raw file
  1. /*
  2. * Date prototype extensions. Doesn't depend on any
  3. * other code. Doens't overwrite existing methods.
  4. *
  5. * Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
  6. * isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
  7. * setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
  8. *
  9. * Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  10. *
  11. * Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
  12. * I've added my name to these methods so you know who to blame if they are broken!
  13. *
  14. * Dual licensed under the MIT and GPL licenses:
  15. * http://www.opensource.org/licenses/mit-license.php
  16. * http://www.gnu.org/licenses/gpl.html
  17. *
  18. */
  19. /**
  20. * An Array of day names starting with Sunday.
  21. *
  22. * @example dayNames[0]
  23. * @result 'Sunday'
  24. *
  25. * @name dayNames
  26. * @type Array
  27. * @cat Plugins/Methods/Date
  28. */
  29. //Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  30. Date.dayNames = ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'];
  31. /**
  32. * An Array of abbreviated day names starting with Sun.
  33. *
  34. * @example abbrDayNames[0]
  35. * @result 'Sun'
  36. *
  37. * @name abbrDayNames
  38. * @type Array
  39. * @cat Plugins/Methods/Date
  40. */
  41. //Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  42. Date.abbrDayNames = ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'];
  43. /**
  44. * An Array of month names starting with Janurary.
  45. *
  46. * @example monthNames[0]
  47. * @result 'January'
  48. *
  49. * @name monthNames
  50. * @type Array
  51. * @cat Plugins/Methods/Date
  52. */
  53. //Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  54. Date.monthNames = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
  55. /**
  56. * An Array of abbreviated month names starting with Jan.
  57. *
  58. * @example abbrMonthNames[0]
  59. * @result 'Jan'
  60. *
  61. * @name monthNames
  62. * @type Array
  63. * @cat Plugins/Methods/Date
  64. */
  65. Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  66. /**
  67. * The first day of the week for this locale.
  68. *
  69. * @name firstDayOfWeek
  70. * @type Number
  71. * @cat Plugins/Methods/Date
  72. * @author Kelvin Luck
  73. */
  74. Date.firstDayOfWeek = 1;
  75. /**
  76. * The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
  77. *
  78. * @name format
  79. * @type String
  80. * @cat Plugins/Methods/Date
  81. * @author Kelvin Luck
  82. */
  83. Date.format = 'dd/mm/yyyy';
  84. //Date.format = 'mm/dd/yyyy';
  85. //Date.format = 'yyyy-mm-dd';
  86. //Date.format = 'dd mmm yy';
  87. /**
  88. * The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
  89. * only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
  90. *
  91. * @name format
  92. * @type String
  93. * @cat Plugins/Methods/Date
  94. * @author Kelvin Luck
  95. */
  96. Date.fullYearStart = '20';
  97. (function() {
  98. /**
  99. * Adds a given method under the given name
  100. * to the Date prototype if it doesn't
  101. * currently exist.
  102. *
  103. * @private
  104. */
  105. function add(name, method) {
  106. if( !Date.prototype[name] ) {
  107. Date.prototype[name] = method;
  108. }
  109. };
  110. /**
  111. * Checks if the year is a leap year.
  112. *
  113. * @example var dtm = new Date("01/12/2008");
  114. * dtm.isLeapYear();
  115. * @result true
  116. *
  117. * @name isLeapYear
  118. * @type Boolean
  119. * @cat Plugins/Methods/Date
  120. */
  121. add("isLeapYear", function() {
  122. var y = this.getFullYear();
  123. return (y%4==0 && y%100!=0) || y%400==0;
  124. });
  125. /**
  126. * Checks if the day is a weekend day (Sat or Sun).
  127. *
  128. * @example var dtm = new Date("01/12/2008");
  129. * dtm.isWeekend();
  130. * @result false
  131. *
  132. * @name isWeekend
  133. * @type Boolean
  134. * @cat Plugins/Methods/Date
  135. */
  136. add("isWeekend", function() {
  137. return this.getDay()==0 || this.getDay()==6;
  138. });
  139. /**
  140. * Check if the day is a day of the week (Mon-Fri)
  141. *
  142. * @example var dtm = new Date("01/12/2008");
  143. * dtm.isWeekDay();
  144. * @result false
  145. *
  146. * @name isWeekDay
  147. * @type Boolean
  148. * @cat Plugins/Methods/Date
  149. */
  150. add("isWeekDay", function() {
  151. return !this.isWeekend();
  152. });
  153. /**
  154. * Gets the number of days in the month.
  155. *
  156. * @example var dtm = new Date("01/12/2008");
  157. * dtm.getDaysInMonth();
  158. * @result 31
  159. *
  160. * @name getDaysInMonth
  161. * @type Number
  162. * @cat Plugins/Methods/Date
  163. */
  164. add("getDaysInMonth", function() {
  165. return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
  166. });
  167. /**
  168. * Gets the name of the day.
  169. *
  170. * @example var dtm = new Date("01/12/2008");
  171. * dtm.getDayName();
  172. * @result 'Saturday'
  173. *
  174. * @example var dtm = new Date("01/12/2008");
  175. * dtm.getDayName(true);
  176. * @result 'Sat'
  177. *
  178. * @param abbreviated Boolean When set to true the name will be abbreviated.
  179. * @name getDayName
  180. * @type String
  181. * @cat Plugins/Methods/Date
  182. */
  183. add("getDayName", function(abbreviated) {
  184. return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
  185. });
  186. /**
  187. * Gets the name of the month.
  188. *
  189. * @example var dtm = new Date("01/12/2008");
  190. * dtm.getMonthName();
  191. * @result 'Janurary'
  192. *
  193. * @example var dtm = new Date("01/12/2008");
  194. * dtm.getMonthName(true);
  195. * @result 'Jan'
  196. *
  197. * @param abbreviated Boolean When set to true the name will be abbreviated.
  198. * @name getDayName
  199. * @type String
  200. * @cat Plugins/Methods/Date
  201. */
  202. add("getMonthName", function(abbreviated) {
  203. return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
  204. });
  205. /**
  206. * Get the number of the day of the year.
  207. *
  208. * @example var dtm = new Date("01/12/2008");
  209. * dtm.getDayOfYear();
  210. * @result 11
  211. *
  212. * @name getDayOfYear
  213. * @type Number
  214. * @cat Plugins/Methods/Date
  215. */
  216. add("getDayOfYear", function() {
  217. var tmpdtm = new Date("1/1/" + this.getFullYear());
  218. return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
  219. });
  220. /**
  221. * Get the number of the week of the year.
  222. *
  223. * @example var dtm = new Date("01/12/2008");
  224. * dtm.getWeekOfYear();
  225. * @result 2
  226. *
  227. * @name getWeekOfYear
  228. * @type Number
  229. * @cat Plugins/Methods/Date
  230. */
  231. add("getWeekOfYear", function() {
  232. return Math.ceil(this.getDayOfYear() / 7);
  233. });
  234. /**
  235. * Set the day of the year.
  236. *
  237. * @example var dtm = new Date("01/12/2008");
  238. * dtm.setDayOfYear(1);
  239. * dtm.toString();
  240. * @result 'Tue Jan 01 2008 00:00:00'
  241. *
  242. * @name setDayOfYear
  243. * @type Date
  244. * @cat Plugins/Methods/Date
  245. */
  246. add("setDayOfYear", function(day) {
  247. this.setMonth(0);
  248. this.setDate(day);
  249. return this;
  250. });
  251. /**
  252. * Add a number of years to the date object.
  253. *
  254. * @example var dtm = new Date("01/12/2008");
  255. * dtm.addYears(1);
  256. * dtm.toString();
  257. * @result 'Mon Jan 12 2009 00:00:00'
  258. *
  259. * @name addYears
  260. * @type Date
  261. * @cat Plugins/Methods/Date
  262. */
  263. add("addYears", function(num) {
  264. this.setFullYear(this.getFullYear() + num);
  265. return this;
  266. });
  267. /**
  268. * Add a number of months to the date object.
  269. *
  270. * @example var dtm = new Date("01/12/2008");
  271. * dtm.addMonths(1);
  272. * dtm.toString();
  273. * @result 'Tue Feb 12 2008 00:00:00'
  274. *
  275. * @name addMonths
  276. * @type Date
  277. * @cat Plugins/Methods/Date
  278. */
  279. add("addMonths", function(num) {
  280. var tmpdtm = this.getDate();
  281. this.setMonth(this.getMonth() + num);
  282. if (tmpdtm > this.getDate())
  283. this.addDays(-this.getDate());
  284. return this;
  285. });
  286. /**
  287. * Add a number of days to the date object.
  288. *
  289. * @example var dtm = new Date("01/12/2008");
  290. * dtm.addDays(1);
  291. * dtm.toString();
  292. * @result 'Sun Jan 13 2008 00:00:00'
  293. *
  294. * @name addDays
  295. * @type Date
  296. * @cat Plugins/Methods/Date
  297. */
  298. add("addDays", function(num) {
  299. //this.setDate(this.getDate() + num);
  300. this.setTime(this.getTime() + (num*86400000) );
  301. return this;
  302. });
  303. /**
  304. * Add a number of hours to the date object.
  305. *
  306. * @example var dtm = new Date("01/12/2008");
  307. * dtm.addHours(24);
  308. * dtm.toString();
  309. * @result 'Sun Jan 13 2008 00:00:00'
  310. *
  311. * @name addHours
  312. * @type Date
  313. * @cat Plugins/Methods/Date
  314. */
  315. add("addHours", function(num) {
  316. this.setHours(this.getHours() + num);
  317. return this;
  318. });
  319. /**
  320. * Add a number of minutes to the date object.
  321. *
  322. * @example var dtm = new Date("01/12/2008");
  323. * dtm.addMinutes(60);
  324. * dtm.toString();
  325. * @result 'Sat Jan 12 2008 01:00:00'
  326. *
  327. * @name addMinutes
  328. * @type Date
  329. * @cat Plugins/Methods/Date
  330. */
  331. add("addMinutes", function(num) {
  332. this.setMinutes(this.getMinutes() + num);
  333. return this;
  334. });
  335. /**
  336. * Add a number of seconds to the date object.
  337. *
  338. * @example var dtm = new Date("01/12/2008");
  339. * dtm.addSeconds(60);
  340. * dtm.toString();
  341. * @result 'Sat Jan 12 2008 00:01:00'
  342. *
  343. * @name addSeconds
  344. * @type Date
  345. * @cat Plugins/Methods/Date
  346. */
  347. add("addSeconds", function(num) {
  348. this.setSeconds(this.getSeconds() + num);
  349. return this;
  350. });
  351. /**
  352. * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
  353. *
  354. * @example var dtm = new Date();
  355. * dtm.zeroTime();
  356. * dtm.toString();
  357. * @result 'Sat Jan 12 2008 00:01:00'
  358. *
  359. * @name zeroTime
  360. * @type Date
  361. * @cat Plugins/Methods/Date
  362. * @author Kelvin Luck
  363. */
  364. add("zeroTime", function() {
  365. this.setMilliseconds(0);
  366. this.setSeconds(0);
  367. this.setMinutes(0);
  368. this.setHours(0);
  369. return this;
  370. });
  371. /**
  372. * Returns a string representation of the date object according to Date.format.
  373. * (Date.toString may be used in other places so I purposefully didn't overwrite it)
  374. *
  375. * @example var dtm = new Date("01/12/2008");
  376. * dtm.asString();
  377. * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
  378. *
  379. * @name asString
  380. * @type Date
  381. * @cat Plugins/Methods/Date
  382. * @author Kelvin Luck
  383. */
  384. add("asString", function(format) {
  385. var r = format || Date.format;
  386. return r
  387. .split('yyyy').join(this.getFullYear())
  388. .split('yy').join((this.getFullYear() + '').substring(2))
  389. .split('mmmm').join(this.getMonthName(false))
  390. .split('mmm').join(this.getMonthName(true))
  391. .split('mm').join(_zeroPad(this.getMonth()+1))
  392. .split('dd').join(_zeroPad(this.getDate()))
  393. .split('hh').join(_zeroPad(this.getHours()))
  394. .split('min').join(_zeroPad(this.getMinutes()))
  395. .split('ss').join(_zeroPad(this.getSeconds()));
  396. });
  397. /**
  398. * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
  399. * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
  400. *
  401. * @example var dtm = Date.fromString("12/01/2008");
  402. * dtm.toString();
  403. * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
  404. *
  405. * @name fromString
  406. * @type Date
  407. * @cat Plugins/Methods/Date
  408. * @author Kelvin Luck
  409. */
  410. Date.fromString = function(s, format)
  411. {
  412. var f = format || Date.format;
  413. var d = new Date('01/01/1977');
  414. var mLength = 0;
  415. var iM = f.indexOf('mmmm');
  416. if (iM > -1) {
  417. for (var i=0; i<Date.monthNames.length; i++) {
  418. var mStr = s.substr(iM, Date.monthNames[i].length);
  419. if (Date.monthNames[i] == mStr) {
  420. mLength = Date.monthNames[i].length - 4;
  421. break;
  422. }
  423. }
  424. d.setMonth(i);
  425. } else {
  426. iM = f.indexOf('mmm');
  427. if (iM > -1) {
  428. var mStr = s.substr(iM, 3);
  429. for (var i=0; i<Date.abbrMonthNames.length; i++) {
  430. if (Date.abbrMonthNames[i] == mStr) break;
  431. }
  432. d.setMonth(i);
  433. } else {
  434. d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
  435. }
  436. }
  437. var iY = f.indexOf('yyyy');
  438. if (iY > -1) {
  439. if (iM < iY)
  440. {
  441. iY += mLength;
  442. }
  443. d.setFullYear(Number(s.substr(iY, 4)));
  444. } else {
  445. if (iM < iY)
  446. {
  447. iY += mLength;
  448. }
  449. // TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
  450. d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
  451. }
  452. var iD = f.indexOf('dd');
  453. if (iM < iD)
  454. {
  455. iD += mLength;
  456. }
  457. d.setDate(Number(s.substr(iD, 2)));
  458. if (isNaN(d.getTime())) {
  459. return false;
  460. }
  461. return d;
  462. };
  463. // utility method
  464. var _zeroPad = function(num) {
  465. var s = '0'+num;
  466. return s.substring(s.length-2)
  467. //return ('0'+num).substring(-2); // doesn't work on IE :(
  468. };
  469. })();