date.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. return this;
  298. });
  299. /**
  300. * Add a number of hours to the date object.
  301. *
  302. * @example var dtm = new Date("01/12/2008");
  303. * dtm.addHours(24);
  304. * dtm.toString();
  305. * @result 'Sun Jan 13 2008 00:00:00'
  306. *
  307. * @name addHours
  308. * @type Date
  309. * @cat Plugins/Methods/Date
  310. */
  311. add("addHours", function(num) {
  312. this.setHours(this.getHours() + num);
  313. return this;
  314. });
  315. /**
  316. * Add a number of minutes to the date object.
  317. *
  318. * @example var dtm = new Date("01/12/2008");
  319. * dtm.addMinutes(60);
  320. * dtm.toString();
  321. * @result 'Sat Jan 12 2008 01:00:00'
  322. *
  323. * @name addMinutes
  324. * @type Date
  325. * @cat Plugins/Methods/Date
  326. */
  327. add("addMinutes", function(num) {
  328. this.setMinutes(this.getMinutes() + num);
  329. return this;
  330. });
  331. /**
  332. * Add a number of seconds to the date object.
  333. *
  334. * @example var dtm = new Date("01/12/2008");
  335. * dtm.addSeconds(60);
  336. * dtm.toString();
  337. * @result 'Sat Jan 12 2008 00:01:00'
  338. *
  339. * @name addSeconds
  340. * @type Date
  341. * @cat Plugins/Methods/Date
  342. */
  343. add("addSeconds", function(num) {
  344. this.setSeconds(this.getSeconds() + num);
  345. return this;
  346. });
  347. /**
  348. * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
  349. *
  350. * @example var dtm = new Date();
  351. * dtm.zeroTime();
  352. * dtm.toString();
  353. * @result 'Sat Jan 12 2008 00:01:00'
  354. *
  355. * @name zeroTime
  356. * @type Date
  357. * @cat Plugins/Methods/Date
  358. * @author Kelvin Luck
  359. */
  360. add("zeroTime", function() {
  361. this.setMilliseconds(0);
  362. this.setSeconds(0);
  363. this.setMinutes(0);
  364. this.setHours(0);
  365. return this;
  366. });
  367. /**
  368. * Returns a string representation of the date object according to Date.format.
  369. * (Date.toString may be used in other places so I purposefully didn't overwrite it)
  370. *
  371. * @example var dtm = new Date("01/12/2008");
  372. * dtm.asString();
  373. * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
  374. *
  375. * @name asString
  376. * @type Date
  377. * @cat Plugins/Methods/Date
  378. * @author Kelvin Luck
  379. */
  380. add("asString", function() {
  381. var r = Date.format;
  382. return r
  383. .split('yyyy').join(this.getFullYear())
  384. .split('yy').join((this.getFullYear() + '').substring(2))
  385. .split('mmm').join(this.getMonthName(true))
  386. .split('mm').join(_zeroPad(this.getMonth()+1))
  387. .split('dd').join(_zeroPad(this.getDate()));
  388. });
  389. /**
  390. * 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
  391. * (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)
  392. *
  393. * @example var dtm = Date.fromString("12/01/2008");
  394. * dtm.toString();
  395. * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
  396. *
  397. * @name fromString
  398. * @type Date
  399. * @cat Plugins/Methods/Date
  400. * @author Kelvin Luck
  401. */
  402. Date.fromString = function(s)
  403. {
  404. var f = Date.format;
  405. var d = new Date('01/01/1977');
  406. var iY = f.indexOf('yyyy');
  407. if (iY > -1) {
  408. d.setFullYear(Number(s.substr(iY, 4)));
  409. } else {
  410. // TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
  411. d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
  412. }
  413. var iM = f.indexOf('mmm');
  414. if (iM > -1) {
  415. var mStr = s.substr(iM, 3);
  416. for (var i=0; i<Date.abbrMonthNames.length; i++) {
  417. if (Date.abbrMonthNames[i] == mStr) break;
  418. }
  419. d.setMonth(i);
  420. } else {
  421. d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
  422. }
  423. d.setDate(Number(s.substr(f.indexOf('dd'), 2)));
  424. if (isNaN(d.getTime())) {
  425. return false;
  426. }
  427. return d;
  428. };
  429. // utility method
  430. var _zeroPad = function(num) {
  431. var s = '0'+num;
  432. return s.substring(s.length-2)
  433. //return ('0'+num).substring(-2); // doesn't work on IE :(
  434. };
  435. })();