PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/javascript/jquery.date.js

https://github.com/lenix/EventCalendar
JavaScript | 420 lines | 114 code | 29 blank | 277 comment | 13 complexity | 2d49363b03f510e90e98d4f7a6216403 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. * The first day of the week for this locale.
  21. *
  22. * @name firstDayOfWeek
  23. * @type Number
  24. * @cat Plugins/Methods/Date
  25. * @author Kelvin Luck
  26. */
  27. Date.firstDayOfWeek = 1;
  28. /**
  29. * 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).
  30. *
  31. * @name format
  32. * @type String
  33. * @cat Plugins/Methods/Date
  34. * @author Kelvin Luck
  35. */
  36. Date.format = 'dd/mm/yyyy';
  37. //Date.format = 'mm/dd/yyyy';
  38. //Date.format = 'yyyy-mm-dd';
  39. //Date.format = 'dd mmm yy';
  40. /**
  41. * 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
  42. * 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.
  43. *
  44. * @name format
  45. * @type String
  46. * @cat Plugins/Methods/Date
  47. * @author Kelvin Luck
  48. */
  49. Date.fullYearStart = '20';
  50. (function() {
  51. /**
  52. * Adds a given method under the given name
  53. * to the Date prototype if it doesn't
  54. * currently exist.
  55. *
  56. * @private
  57. */
  58. function add(name, method) {
  59. if( !Date.prototype[name] ) {
  60. Date.prototype[name] = method;
  61. }
  62. };
  63. /**
  64. * Checks if the year is a leap year.
  65. *
  66. * @example var dtm = new Date("01/12/2008");
  67. * dtm.isLeapYear();
  68. * @result true
  69. *
  70. * @name isLeapYear
  71. * @type Boolean
  72. * @cat Plugins/Methods/Date
  73. */
  74. add("isLeapYear", function() {
  75. var y = this.getFullYear();
  76. return (y%4==0 && y%100!=0) || y%400==0;
  77. });
  78. /**
  79. * Checks if the day is a weekend day (Sat or Sun).
  80. *
  81. * @example var dtm = new Date("01/12/2008");
  82. * dtm.isWeekend();
  83. * @result false
  84. *
  85. * @name isWeekend
  86. * @type Boolean
  87. * @cat Plugins/Methods/Date
  88. */
  89. add("isWeekend", function() {
  90. return this.getDay()==0 || this.getDay()==6;
  91. });
  92. /**
  93. * Check if the day is a day of the week (Mon-Fri)
  94. *
  95. * @example var dtm = new Date("01/12/2008");
  96. * dtm.isWeekDay();
  97. * @result false
  98. *
  99. * @name isWeekDay
  100. * @type Boolean
  101. * @cat Plugins/Methods/Date
  102. */
  103. add("isWeekDay", function() {
  104. return !this.isWeekend();
  105. });
  106. /**
  107. * Gets the number of days in the month.
  108. *
  109. * @example var dtm = new Date("01/12/2008");
  110. * dtm.getDaysInMonth();
  111. * @result 31
  112. *
  113. * @name getDaysInMonth
  114. * @type Number
  115. * @cat Plugins/Methods/Date
  116. */
  117. add("getDaysInMonth", function() {
  118. return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
  119. });
  120. /**
  121. * Gets the name of the day.
  122. *
  123. * @example var dtm = new Date("01/12/2008");
  124. * dtm.getDayName();
  125. * @result 'Saturday'
  126. *
  127. * @example var dtm = new Date("01/12/2008");
  128. * dtm.getDayName(true);
  129. * @result 'Sat'
  130. *
  131. * @param abbreviated Boolean When set to true the name will be abbreviated.
  132. * @name getDayName
  133. * @type String
  134. * @cat Plugins/Methods/Date
  135. */
  136. add("getDayName", function(abbreviated) {
  137. return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
  138. });
  139. /**
  140. * Gets the name of the month.
  141. *
  142. * @example var dtm = new Date("01/12/2008");
  143. * dtm.getMonthName();
  144. * @result 'Janurary'
  145. *
  146. * @example var dtm = new Date("01/12/2008");
  147. * dtm.getMonthName(true);
  148. * @result 'Jan'
  149. *
  150. * @param abbreviated Boolean When set to true the name will be abbreviated.
  151. * @name getDayName
  152. * @type String
  153. * @cat Plugins/Methods/Date
  154. */
  155. add("getMonthName", function(abbreviated) {
  156. return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
  157. });
  158. /**
  159. * Get the number of the day of the year.
  160. *
  161. * @example var dtm = new Date("01/12/2008");
  162. * dtm.getDayOfYear();
  163. * @result 11
  164. *
  165. * @name getDayOfYear
  166. * @type Number
  167. * @cat Plugins/Methods/Date
  168. */
  169. add("getDayOfYear", function() {
  170. var tmpdtm = new Date("1/1/" + this.getFullYear());
  171. return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
  172. });
  173. /**
  174. * Get the number of the week of the year.
  175. *
  176. * @example var dtm = new Date("01/12/2008");
  177. * dtm.getWeekOfYear();
  178. * @result 2
  179. *
  180. * @name getWeekOfYear
  181. * @type Number
  182. * @cat Plugins/Methods/Date
  183. */
  184. add("getWeekOfYear", function() {
  185. return Math.ceil(this.getDayOfYear() / 7);
  186. });
  187. /**
  188. * Set the day of the year.
  189. *
  190. * @example var dtm = new Date("01/12/2008");
  191. * dtm.setDayOfYear(1);
  192. * dtm.toString();
  193. * @result 'Tue Jan 01 2008 00:00:00'
  194. *
  195. * @name setDayOfYear
  196. * @type Date
  197. * @cat Plugins/Methods/Date
  198. */
  199. add("setDayOfYear", function(day) {
  200. this.setMonth(0);
  201. this.setDate(day);
  202. return this;
  203. });
  204. /**
  205. * Add a number of years to the date object.
  206. *
  207. * @example var dtm = new Date("01/12/2008");
  208. * dtm.addYears(1);
  209. * dtm.toString();
  210. * @result 'Mon Jan 12 2009 00:00:00'
  211. *
  212. * @name addYears
  213. * @type Date
  214. * @cat Plugins/Methods/Date
  215. */
  216. add("addYears", function(num) {
  217. this.setFullYear(this.getFullYear() + num);
  218. return this;
  219. });
  220. /**
  221. * Add a number of months to the date object.
  222. *
  223. * @example var dtm = new Date("01/12/2008");
  224. * dtm.addMonths(1);
  225. * dtm.toString();
  226. * @result 'Tue Feb 12 2008 00:00:00'
  227. *
  228. * @name addMonths
  229. * @type Date
  230. * @cat Plugins/Methods/Date
  231. */
  232. add("addMonths", function(num) {
  233. var tmpdtm = this.getDate();
  234. this.setMonth(this.getMonth() + num);
  235. if (tmpdtm > this.getDate())
  236. this.addDays(-this.getDate());
  237. return this;
  238. });
  239. /**
  240. * Add a number of days to the date object.
  241. *
  242. * @example var dtm = new Date("01/12/2008");
  243. * dtm.addDays(1);
  244. * dtm.toString();
  245. * @result 'Sun Jan 13 2008 00:00:00'
  246. *
  247. * @name addDays
  248. * @type Date
  249. * @cat Plugins/Methods/Date
  250. */
  251. add("addDays", function(num) {
  252. this.setDate(this.getDate() + num);
  253. return this;
  254. });
  255. /**
  256. * Add a number of hours to the date object.
  257. *
  258. * @example var dtm = new Date("01/12/2008");
  259. * dtm.addHours(24);
  260. * dtm.toString();
  261. * @result 'Sun Jan 13 2008 00:00:00'
  262. *
  263. * @name addHours
  264. * @type Date
  265. * @cat Plugins/Methods/Date
  266. */
  267. add("addHours", function(num) {
  268. this.setHours(this.getHours() + num);
  269. return this;
  270. });
  271. /**
  272. * Add a number of minutes to the date object.
  273. *
  274. * @example var dtm = new Date("01/12/2008");
  275. * dtm.addMinutes(60);
  276. * dtm.toString();
  277. * @result 'Sat Jan 12 2008 01:00:00'
  278. *
  279. * @name addMinutes
  280. * @type Date
  281. * @cat Plugins/Methods/Date
  282. */
  283. add("addMinutes", function(num) {
  284. this.setMinutes(this.getMinutes() + num);
  285. return this;
  286. });
  287. /**
  288. * Add a number of seconds to the date object.
  289. *
  290. * @example var dtm = new Date("01/12/2008");
  291. * dtm.addSeconds(60);
  292. * dtm.toString();
  293. * @result 'Sat Jan 12 2008 00:01:00'
  294. *
  295. * @name addSeconds
  296. * @type Date
  297. * @cat Plugins/Methods/Date
  298. */
  299. add("addSeconds", function(num) {
  300. this.setSeconds(this.getSeconds() + num);
  301. return this;
  302. });
  303. /**
  304. * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
  305. *
  306. * @example var dtm = new Date();
  307. * dtm.zeroTime();
  308. * dtm.toString();
  309. * @result 'Sat Jan 12 2008 00:01:00'
  310. *
  311. * @name zeroTime
  312. * @type Date
  313. * @cat Plugins/Methods/Date
  314. * @author Kelvin Luck
  315. */
  316. add("zeroTime", function() {
  317. this.setMilliseconds(0);
  318. this.setSeconds(0);
  319. this.setMinutes(0);
  320. this.setHours(0);
  321. return this;
  322. });
  323. /**
  324. * Returns a string representation of the date object according to Date.format.
  325. * (Date.toString may be used in other places so I purposefully didn't overwrite it)
  326. *
  327. * @example var dtm = new Date("01/12/2008");
  328. * dtm.asString();
  329. * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
  330. *
  331. * @name asString
  332. * @type Date
  333. * @cat Plugins/Methods/Date
  334. * @author Kelvin Luck
  335. */
  336. add("asString", function() {
  337. var r = Date.format;
  338. return r
  339. .split('yyyy').join(this.getFullYear())
  340. .split('yy').join((this.getFullYear() + '').substring(2))
  341. .split('mmm').join(this.getMonthName(true))
  342. .split('mm').join(_zeroPad(this.getMonth()+1))
  343. .split('dd').join(_zeroPad(this.getDate()));
  344. });
  345. /**
  346. * 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
  347. * (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)
  348. *
  349. * @example var dtm = Date.fromString("12/01/2008");
  350. * dtm.toString();
  351. * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
  352. *
  353. * @name fromString
  354. * @type Date
  355. * @cat Plugins/Methods/Date
  356. * @author Kelvin Luck
  357. */
  358. Date.fromString = function(s)
  359. {
  360. var f = Date.format;
  361. var d = new Date('01/01/1977');
  362. var iY = f.indexOf('yyyy');
  363. if (iY > -1) {
  364. d.setFullYear(Number(s.substr(iY, 4)));
  365. } else {
  366. // TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
  367. d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
  368. }
  369. var iM = f.indexOf('mmm');
  370. if (iM > -1) {
  371. var mStr = s.substr(iM, 3);
  372. for (var i=0; i<Date.abbrMonthNames.length; i++) {
  373. if (Date.abbrMonthNames[i] == mStr) break;
  374. }
  375. d.setMonth(i);
  376. } else {
  377. d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
  378. }
  379. d.setDate(Number(s.substr(f.indexOf('dd'), 2)));
  380. if (isNaN(d.getTime())) {
  381. return false;
  382. }
  383. return d;
  384. };
  385. // utility method
  386. var _zeroPad = function(num) {
  387. var s = '0'+num;
  388. return s.substring(s.length-2)
  389. //return ('0'+num).substring(-2); // doesn't work on IE :(
  390. };
  391. })();