PageRenderTime 102ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-content/themes/twentytwelve-child/js/date.js

https://bitbucket.org/antonyravel/cape-resorts
JavaScript | 507 lines | 153 code | 37 blank | 317 comment | 18 complexity | b5e01d3a6463c0c324ce4144bc581701 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. /**
  31. * An Array of abbreviated day names starting with Sun.
  32. *
  33. * @example abbrDayNames[0]
  34. * @result 'Sun'
  35. *
  36. * @name abbrDayNames
  37. * @type Array
  38. * @cat Plugins/Methods/Date
  39. */
  40. Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  41. /**
  42. * An Array of month names starting with Janurary.
  43. *
  44. * @example monthNames[0]
  45. * @result 'January'
  46. *
  47. * @name monthNames
  48. * @type Array
  49. * @cat Plugins/Methods/Date
  50. */
  51. Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  52. /**
  53. * An Array of abbreviated month names starting with Jan.
  54. *
  55. * @example abbrMonthNames[0]
  56. * @result 'Jan'
  57. *
  58. * @name monthNames
  59. * @type Array
  60. * @cat Plugins/Methods/Date
  61. */
  62. Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  63. /**
  64. * The first day of the week for this locale.
  65. *
  66. * @name firstDayOfWeek
  67. * @type Number
  68. * @cat Plugins/Methods/Date
  69. * @author Kelvin Luck
  70. */
  71. Date.firstDayOfWeek = 1;
  72. /**
  73. * 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).
  74. *
  75. * @name format
  76. * @type String
  77. * @cat Plugins/Methods/Date
  78. * @author Kelvin Luck
  79. */
  80. Date.format = 'dd/mm/yyyy';
  81. //Date.format = 'mm/dd/yyyy';
  82. //Date.format = 'yyyy-mm-dd';
  83. //Date.format = 'dd mmm yy';
  84. /**
  85. * 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
  86. * 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.
  87. *
  88. * @name format
  89. * @type String
  90. * @cat Plugins/Methods/Date
  91. * @author Kelvin Luck
  92. */
  93. Date.fullYearStart = '20';
  94. (function() {
  95. /**
  96. * Adds a given method under the given name
  97. * to the Date prototype if it doesn't
  98. * currently exist.
  99. *
  100. * @private
  101. */
  102. function add(name, method) {
  103. if( !Date.prototype[name] ) {
  104. Date.prototype[name] = method;
  105. }
  106. };
  107. /**
  108. * Checks if the year is a leap year.
  109. *
  110. * @example var dtm = new Date("01/12/2008");
  111. * dtm.isLeapYear();
  112. * @result true
  113. *
  114. * @name isLeapYear
  115. * @type Boolean
  116. * @cat Plugins/Methods/Date
  117. */
  118. add("isLeapYear", function() {
  119. var y = this.getFullYear();
  120. return (y%4==0 && y%100!=0) || y%400==0;
  121. });
  122. /**
  123. * Checks if the day is a weekend day (Sat or Sun).
  124. *
  125. * @example var dtm = new Date("01/12/2008");
  126. * dtm.isWeekend();
  127. * @result false
  128. *
  129. * @name isWeekend
  130. * @type Boolean
  131. * @cat Plugins/Methods/Date
  132. */
  133. add("isWeekend", function() {
  134. return this.getDay()==0 || this.getDay()==6;
  135. });
  136. /**
  137. * Check if the day is a day of the week (Mon-Fri)
  138. *
  139. * @example var dtm = new Date("01/12/2008");
  140. * dtm.isWeekDay();
  141. * @result false
  142. *
  143. * @name isWeekDay
  144. * @type Boolean
  145. * @cat Plugins/Methods/Date
  146. */
  147. add("isWeekDay", function() {
  148. return !this.isWeekend();
  149. });
  150. /**
  151. * Gets the number of days in the month.
  152. *
  153. * @example var dtm = new Date("01/12/2008");
  154. * dtm.getDaysInMonth();
  155. * @result 31
  156. *
  157. * @name getDaysInMonth
  158. * @type Number
  159. * @cat Plugins/Methods/Date
  160. */
  161. add("getDaysInMonth", function() {
  162. return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
  163. });
  164. /**
  165. * Gets the name of the day.
  166. *
  167. * @example var dtm = new Date("01/12/2008");
  168. * dtm.getDayName();
  169. * @result 'Saturday'
  170. *
  171. * @example var dtm = new Date("01/12/2008");
  172. * dtm.getDayName(true);
  173. * @result 'Sat'
  174. *
  175. * @param abbreviated Boolean When set to true the name will be abbreviated.
  176. * @name getDayName
  177. * @type String
  178. * @cat Plugins/Methods/Date
  179. */
  180. add("getDayName", function(abbreviated) {
  181. return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
  182. });
  183. /**
  184. * Gets the name of the month.
  185. *
  186. * @example var dtm = new Date("01/12/2008");
  187. * dtm.getMonthName();
  188. * @result 'Janurary'
  189. *
  190. * @example var dtm = new Date("01/12/2008");
  191. * dtm.getMonthName(true);
  192. * @result 'Jan'
  193. *
  194. * @param abbreviated Boolean When set to true the name will be abbreviated.
  195. * @name getDayName
  196. * @type String
  197. * @cat Plugins/Methods/Date
  198. */
  199. add("getMonthName", function(abbreviated) {
  200. return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
  201. });
  202. /**
  203. * Get the number of the day of the year.
  204. *
  205. * @example var dtm = new Date("01/12/2008");
  206. * dtm.getDayOfYear();
  207. * @result 11
  208. *
  209. * @name getDayOfYear
  210. * @type Number
  211. * @cat Plugins/Methods/Date
  212. */
  213. add("getDayOfYear", function() {
  214. var tmpdtm = new Date("1/1/" + this.getFullYear());
  215. return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
  216. });
  217. /**
  218. * Get the number of the week of the year.
  219. *
  220. * @example var dtm = new Date("01/12/2008");
  221. * dtm.getWeekOfYear();
  222. * @result 2
  223. *
  224. * @name getWeekOfYear
  225. * @type Number
  226. * @cat Plugins/Methods/Date
  227. */
  228. add("getWeekOfYear", function() {
  229. return Math.ceil(this.getDayOfYear() / 7);
  230. });
  231. /**
  232. * Set the day of the year.
  233. *
  234. * @example var dtm = new Date("01/12/2008");
  235. * dtm.setDayOfYear(1);
  236. * dtm.toString();
  237. * @result 'Tue Jan 01 2008 00:00:00'
  238. *
  239. * @name setDayOfYear
  240. * @type Date
  241. * @cat Plugins/Methods/Date
  242. */
  243. add("setDayOfYear", function(day) {
  244. this.setMonth(0);
  245. this.setDate(day);
  246. return this;
  247. });
  248. /**
  249. * Add a number of years to the date object.
  250. *
  251. * @example var dtm = new Date("01/12/2008");
  252. * dtm.addYears(1);
  253. * dtm.toString();
  254. * @result 'Mon Jan 12 2009 00:00:00'
  255. *
  256. * @name addYears
  257. * @type Date
  258. * @cat Plugins/Methods/Date
  259. */
  260. add("addYears", function(num) {
  261. this.setFullYear(this.getFullYear() + num);
  262. return this;
  263. });
  264. /**
  265. * Add a number of months to the date object.
  266. *
  267. * @example var dtm = new Date("01/12/2008");
  268. * dtm.addMonths(1);
  269. * dtm.toString();
  270. * @result 'Tue Feb 12 2008 00:00:00'
  271. *
  272. * @name addMonths
  273. * @type Date
  274. * @cat Plugins/Methods/Date
  275. */
  276. add("addMonths", function(num) {
  277. var tmpdtm = this.getDate();
  278. this.setMonth(this.getMonth() + num);
  279. if (tmpdtm > this.getDate())
  280. this.addDays(-this.getDate());
  281. return this;
  282. });
  283. /**
  284. * Add a number of days to the date object.
  285. *
  286. * @example var dtm = new Date("01/12/2008");
  287. * dtm.addDays(1);
  288. * dtm.toString();
  289. * @result 'Sun Jan 13 2008 00:00:00'
  290. *
  291. * @name addDays
  292. * @type Date
  293. * @cat Plugins/Methods/Date
  294. */
  295. add("addDays", function(num) {
  296. //this.setDate(this.getDate() + num);
  297. this.setTime(this.getTime() + (num*86400000) );
  298. return this;
  299. });
  300. /**
  301. * Add a number of hours to the date object.
  302. *
  303. * @example var dtm = new Date("01/12/2008");
  304. * dtm.addHours(24);
  305. * dtm.toString();
  306. * @result 'Sun Jan 13 2008 00:00:00'
  307. *
  308. * @name addHours
  309. * @type Date
  310. * @cat Plugins/Methods/Date
  311. */
  312. add("addHours", function(num) {
  313. this.setHours(this.getHours() + num);
  314. return this;
  315. });
  316. /**
  317. * Add a number of minutes to the date object.
  318. *
  319. * @example var dtm = new Date("01/12/2008");
  320. * dtm.addMinutes(60);
  321. * dtm.toString();
  322. * @result 'Sat Jan 12 2008 01:00:00'
  323. *
  324. * @name addMinutes
  325. * @type Date
  326. * @cat Plugins/Methods/Date
  327. */
  328. add("addMinutes", function(num) {
  329. this.setMinutes(this.getMinutes() + num);
  330. return this;
  331. });
  332. /**
  333. * Add a number of seconds to the date object.
  334. *
  335. * @example var dtm = new Date("01/12/2008");
  336. * dtm.addSeconds(60);
  337. * dtm.toString();
  338. * @result 'Sat Jan 12 2008 00:01:00'
  339. *
  340. * @name addSeconds
  341. * @type Date
  342. * @cat Plugins/Methods/Date
  343. */
  344. add("addSeconds", function(num) {
  345. this.setSeconds(this.getSeconds() + num);
  346. return this;
  347. });
  348. /**
  349. * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
  350. *
  351. * @example var dtm = new Date();
  352. * dtm.zeroTime();
  353. * dtm.toString();
  354. * @result 'Sat Jan 12 2008 00:01:00'
  355. *
  356. * @name zeroTime
  357. * @type Date
  358. * @cat Plugins/Methods/Date
  359. * @author Kelvin Luck
  360. */
  361. add("zeroTime", function() {
  362. this.setMilliseconds(0);
  363. this.setSeconds(0);
  364. this.setMinutes(0);
  365. this.setHours(0);
  366. return this;
  367. });
  368. /**
  369. * Returns a string representation of the date object according to Date.format.
  370. * (Date.toString may be used in other places so I purposefully didn't overwrite it)
  371. *
  372. * @example var dtm = new Date("01/12/2008");
  373. * dtm.asString();
  374. * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
  375. *
  376. * @name asString
  377. * @type Date
  378. * @cat Plugins/Methods/Date
  379. * @author Kelvin Luck
  380. */
  381. add("asString", function(format) {
  382. var r = format || Date.format;
  383. if (r.split('mm').length>1) { // ugly workaround to make sure we don't replace the m's in e.g. noveMber
  384. r = r.split('mmmm').join(this.getMonthName(false))
  385. .split('mmm').join(this.getMonthName(true))
  386. .split('mm').join(_zeroPad(this.getMonth()+1))
  387. } else {
  388. r = r.split('m').join(this.getMonth()+1);
  389. }
  390. r = r.split('yyyy').join(this.getFullYear())
  391. .split('yy').join((this.getFullYear() + '').substring(2))
  392. .split('dd').join(_zeroPad(this.getDate()))
  393. .split('d').join(this.getDate());
  394. return r;
  395. });
  396. /**
  397. * 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
  398. * (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)
  399. *
  400. * @example var dtm = Date.fromString("12/01/2008");
  401. * dtm.toString();
  402. * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
  403. *
  404. * @name fromString
  405. * @type Date
  406. * @cat Plugins/Methods/Date
  407. * @author Kelvin Luck
  408. */
  409. Date.fromString = function(s)
  410. {
  411. var f = Date.format;
  412. var d = new Date('01/01/1970');
  413. if (s == '') return d;
  414. s = s.toLowerCase();
  415. var matcher = '';
  416. var order = [];
  417. var r = /(dd?d?|mm?m?|yy?yy?)+([^(m|d|y)])?/g;
  418. var results;
  419. while ((results = r.exec(f)) != null)
  420. {
  421. switch (results[1]) {
  422. case 'd':
  423. case 'dd':
  424. case 'm':
  425. case 'mm':
  426. case 'yy':
  427. case 'yyyy':
  428. matcher += '(\\d+\\d?\\d?\\d?)+';
  429. order.push(results[1].substr(0, 1));
  430. break;
  431. case 'mmm':
  432. matcher += '([a-z]{3})';
  433. order.push('M');
  434. break;
  435. }
  436. if (results[2]) {
  437. matcher += results[2];
  438. }
  439. }
  440. var dm = new RegExp(matcher);
  441. var result = s.match(dm);
  442. for (var i=0; i<order.length; i++) {
  443. var res = result[i+1];
  444. switch(order[i]) {
  445. case 'd':
  446. d.setDate(res);
  447. break;
  448. case 'm':
  449. d.setMonth(Number(res)-1);
  450. break;
  451. case 'M':
  452. for (var j=0; j<Date.abbrMonthNames.length; j++) {
  453. if (Date.abbrMonthNames[j].toLowerCase() == res) break;
  454. }
  455. d.setMonth(j);
  456. break;
  457. case 'y':
  458. d.setYear(res);
  459. break;
  460. }
  461. }
  462. return d;
  463. };
  464. // utility method
  465. var _zeroPad = function(num) {
  466. var s = '0'+num;
  467. return s.substring(s.length-2)
  468. //return ('0'+num).substring(-2); // doesn't work on IE :(
  469. };
  470. })();