PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/maven.samples/samples_src/MavenScrumToys/src/main/webapp/resources/js/date.js

https://gitlab.com/essere.lab.public/qualitas.class-corpus
JavaScript | 452 lines | 377 code | 17 blank | 58 comment | 120 complexity | 76677fa6ffc6bd539647799e4394e8da MD5 | raw file
  1. /*
  2. * OSR: https://opensourcereview.central.sun.com/app?action=ViewReq&review_type=Expedited&traq_num=12626
  3. * source: http://www.javascripttoolbox.com/libsource.php/date/source/date.js
  4. * version: 1.02
  5. */
  6. /**
  7. * Copyright (c)2005-2009 Matt Kruse (javascripttoolbox.com)
  8. *
  9. * Dual licensed under the MIT and GPL licenses.
  10. * This basically means you can use this code however you want for
  11. * free, but don't claim to have written it yourself!
  12. * Donations always accepted: http://www.JavascriptToolbox.com/donate/
  13. *
  14. * Please do not link to the .js files on javascripttoolbox.com from
  15. * your site. Copy the files locally to your server instead.
  16. *
  17. */
  18. /*
  19. Date functions
  20. These functions are used to parse, format, and manipulate Date objects.
  21. See documentation and examples at http://www.JavascriptToolbox.com/lib/date/
  22. */
  23. Date.$VERSION = 1.02;
  24. // Utility function to append a 0 to single-digit numbers
  25. Date.LZ = function(x) {return(x<0||x>9?"":"0")+x};
  26. // Full month names. Change this for local month names
  27. Date.monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  28. // Month abbreviations. Change this for local month names
  29. Date.monthAbbreviations = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  30. // Full day names. Change this for local month names
  31. Date.dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  32. // Day abbreviations. Change this for local month names
  33. Date.dayAbbreviations = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  34. // Used for parsing ambiguous dates like 1/2/2000 - default to preferring 'American' format meaning Jan 2.
  35. // Set to false to prefer 'European' format meaning Feb 1
  36. Date.preferAmericanFormat = true;
  37. // If the getFullYear() method is not defined, create it
  38. if (!Date.prototype.getFullYear) {
  39. Date.prototype.getFullYear = function() { var yy=this.getYear(); return (yy<1900?yy+1900:yy); } ;
  40. }
  41. // Parse a string and convert it to a Date object.
  42. // If no format is passed, try a list of common formats.
  43. // If string cannot be parsed, return null.
  44. // Avoids regular expressions to be more portable.
  45. Date.parseString = function(val, format) {
  46. // If no format is specified, try a few common formats
  47. if (typeof(format)=="undefined" || format==null || format=="") {
  48. var generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d','MMM-d','d-MMM');
  49. var monthFirst=new Array('M/d/y','M-d-y','M.d.y','M/d','M-d');
  50. var dateFirst =new Array('d/M/y','d-M-y','d.M.y','d/M','d-M');
  51. var checkList=new Array(generalFormats,Date.preferAmericanFormat?monthFirst:dateFirst,Date.preferAmericanFormat?dateFirst:monthFirst);
  52. for (var i=0; i<checkList.length; i++) {
  53. var l=checkList[i];
  54. for (var j=0; j<l.length; j++) {
  55. var d=Date.parseString(val,l[j]);
  56. if (d!=null) {
  57. return d;
  58. }
  59. }
  60. }
  61. return null;
  62. };
  63. this.isInteger = function(val) {
  64. for (var i=0; i < val.length; i++) {
  65. if ("1234567890".indexOf(val.charAt(i))==-1) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. };
  71. this.getInt = function(str,i,minlength,maxlength) {
  72. for (var x=maxlength; x>=minlength; x--) {
  73. var token=str.substring(i,i+x);
  74. if (token.length < minlength) {
  75. return null;
  76. }
  77. if (this.isInteger(token)) {
  78. return token;
  79. }
  80. }
  81. return null;
  82. };
  83. val=val+"";
  84. format=format+"";
  85. var i_val=0;
  86. var i_format=0;
  87. var c="";
  88. var token="";
  89. var token2="";
  90. var x,y;
  91. var year=new Date().getFullYear();
  92. var month=1;
  93. var date=1;
  94. var hh=0;
  95. var mm=0;
  96. var ss=0;
  97. var ampm="";
  98. while (i_format < format.length) {
  99. // Get next token from format string
  100. c=format.charAt(i_format);
  101. token="";
  102. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  103. token += format.charAt(i_format++);
  104. }
  105. // Extract contents of value based on format token
  106. if (token=="yyyy" || token=="yy" || token=="y") {
  107. if (token=="yyyy") {
  108. x=4;y=4;
  109. }
  110. if (token=="yy") {
  111. x=2;y=2;
  112. }
  113. if (token=="y") {
  114. x=2;y=4;
  115. }
  116. year=this.getInt(val,i_val,x,y);
  117. if (year==null) {
  118. return null;
  119. }
  120. i_val += year.length;
  121. if (year.length==2) {
  122. if (year > 70) {
  123. year=1900+(year-0);
  124. }
  125. else {
  126. year=2000+(year-0);
  127. }
  128. }
  129. }
  130. else if (token=="MMM" || token=="NNN"){
  131. month=0;
  132. var names = (token=="MMM"?(Date.monthNames.concat(Date.monthAbbreviations)):Date.monthAbbreviations);
  133. for (var i=0; i<names.length; i++) {
  134. var month_name=names[i];
  135. if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
  136. month=(i%12)+1;
  137. i_val += month_name.length;
  138. break;
  139. }
  140. }
  141. if ((month < 1)||(month>12)){
  142. return null;
  143. }
  144. }
  145. else if (token=="EE"||token=="E"){
  146. var names = (token=="EE"?Date.dayNames:Date.dayAbbreviations);
  147. for (var i=0; i<names.length; i++) {
  148. var day_name=names[i];
  149. if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
  150. i_val += day_name.length;
  151. break;
  152. }
  153. }
  154. }
  155. else if (token=="MM"||token=="M") {
  156. month=this.getInt(val,i_val,token.length,2);
  157. if(month==null||(month<1)||(month>12)){
  158. return null;
  159. }
  160. i_val+=month.length;
  161. }
  162. else if (token=="dd"||token=="d") {
  163. date=this.getInt(val,i_val,token.length,2);
  164. if(date==null||(date<1)||(date>31)){
  165. return null;
  166. }
  167. i_val+=date.length;
  168. }
  169. else if (token=="hh"||token=="h") {
  170. hh=this.getInt(val,i_val,token.length,2);
  171. if(hh==null||(hh<1)||(hh>12)){
  172. return null;
  173. }
  174. i_val+=hh.length;
  175. }
  176. else if (token=="HH"||token=="H") {
  177. hh=this.getInt(val,i_val,token.length,2);
  178. if(hh==null||(hh<0)||(hh>23)){
  179. return null;
  180. }
  181. i_val+=hh.length;
  182. }
  183. else if (token=="KK"||token=="K") {
  184. hh=this.getInt(val,i_val,token.length,2);
  185. if(hh==null||(hh<0)||(hh>11)){
  186. return null;
  187. }
  188. i_val+=hh.length;
  189. hh++;
  190. }
  191. else if (token=="kk"||token=="k") {
  192. hh=this.getInt(val,i_val,token.length,2);
  193. if(hh==null||(hh<1)||(hh>24)){
  194. return null;
  195. }
  196. i_val+=hh.length;
  197. hh--;
  198. }
  199. else if (token=="mm"||token=="m") {
  200. mm=this.getInt(val,i_val,token.length,2);
  201. if(mm==null||(mm<0)||(mm>59)){
  202. return null;
  203. }
  204. i_val+=mm.length;
  205. }
  206. else if (token=="ss"||token=="s") {
  207. ss=this.getInt(val,i_val,token.length,2);
  208. if(ss==null||(ss<0)||(ss>59)){
  209. return null;
  210. }
  211. i_val+=ss.length;
  212. }
  213. else if (token=="a") {
  214. if (val.substring(i_val,i_val+2).toLowerCase()=="am") {
  215. ampm="AM";
  216. }
  217. else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {
  218. ampm="PM";
  219. }
  220. else {
  221. return null;
  222. }
  223. i_val+=2;
  224. }
  225. else {
  226. if (val.substring(i_val,i_val+token.length)!=token) {
  227. return null;
  228. }
  229. else {
  230. i_val+=token.length;
  231. }
  232. }
  233. }
  234. // If there are any trailing characters left in the value, it doesn't match
  235. if (i_val != val.length) {
  236. return null;
  237. }
  238. // Is date valid for month?
  239. if (month==2) {
  240. // Check for leap year
  241. if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
  242. if (date > 29){
  243. return null;
  244. }
  245. }
  246. else {
  247. if (date > 28) {
  248. return null;
  249. }
  250. }
  251. }
  252. if ((month==4)||(month==6)||(month==9)||(month==11)) {
  253. if (date > 30) {
  254. return null;
  255. }
  256. }
  257. // Correct hours value
  258. if (hh<12 && ampm=="PM") {
  259. hh=hh-0+12;
  260. }
  261. else if (hh>11 && ampm=="AM") {
  262. hh-=12;
  263. }
  264. return new Date(year,month-1,date,hh,mm,ss);
  265. };
  266. // Check if a date string is valid
  267. Date.isValid = function(val,format) {
  268. return (Date.parseString(val,format) != null);
  269. };
  270. // Check if a date object is before another date object
  271. Date.prototype.isBefore = function(date2) {
  272. if (date2==null) {
  273. return false;
  274. }
  275. return (this.getTime()<date2.getTime());
  276. };
  277. // Check if a date object is after another date object
  278. Date.prototype.isAfter = function(date2) {
  279. if (date2==null) {
  280. return false;
  281. }
  282. return (this.getTime()>date2.getTime());
  283. };
  284. // Check if two date objects have equal dates and times
  285. Date.prototype.equals = function(date2) {
  286. if (date2==null) {
  287. return false;
  288. }
  289. return (this.getTime()==date2.getTime());
  290. };
  291. // Check if two date objects have equal dates, disregarding times
  292. Date.prototype.equalsIgnoreTime = function(date2) {
  293. if (date2==null) {
  294. return false;
  295. }
  296. var d1 = new Date(this.getTime()).clearTime();
  297. var d2 = new Date(date2.getTime()).clearTime();
  298. return (d1.getTime()==d2.getTime());
  299. };
  300. // Format a date into a string using a given format string
  301. Date.prototype.format = function(format) {
  302. format=format+"";
  303. var result="";
  304. var i_format=0;
  305. var c="";
  306. var token="";
  307. var y=this.getYear()+"";
  308. var M=this.getMonth()+1;
  309. var d=this.getDate();
  310. var E=this.getDay();
  311. var H=this.getHours();
  312. var m=this.getMinutes();
  313. var s=this.getSeconds();
  314. var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
  315. // Convert real date parts into formatted versions
  316. var value=new Object();
  317. if (y.length < 4) {
  318. y=""+(+y+1900);
  319. }
  320. value["y"]=""+y;
  321. value["yyyy"]=y;
  322. value["yy"]=y.substring(2,4);
  323. value["M"]=M;
  324. value["MM"]=Date.LZ(M);
  325. value["MMM"]=Date.monthNames[M-1];
  326. value["NNN"]=Date.monthAbbreviations[M-1];
  327. value["d"]=d;
  328. value["dd"]=Date.LZ(d);
  329. value["E"]=Date.dayAbbreviations[E];
  330. value["EE"]=Date.dayNames[E];
  331. value["H"]=H;
  332. value["HH"]=Date.LZ(H);
  333. if (H==0){
  334. value["h"]=12;
  335. }
  336. else if (H>12){
  337. value["h"]=H-12;
  338. }
  339. else {
  340. value["h"]=H;
  341. }
  342. value["hh"]=Date.LZ(value["h"]);
  343. value["K"]=value["h"]-1;
  344. value["k"]=value["H"]+1;
  345. value["KK"]=Date.LZ(value["K"]);
  346. value["kk"]=Date.LZ(value["k"]);
  347. if (H > 11) {
  348. value["a"]="PM";
  349. }
  350. else {
  351. value["a"]="AM";
  352. }
  353. value["m"]=m;
  354. value["mm"]=Date.LZ(m);
  355. value["s"]=s;
  356. value["ss"]=Date.LZ(s);
  357. while (i_format < format.length) {
  358. c=format.charAt(i_format);
  359. token="";
  360. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  361. token += format.charAt(i_format++);
  362. }
  363. if (typeof(value[token])!="undefined") {
  364. result=result + value[token];
  365. }
  366. else {
  367. result=result + token;
  368. }
  369. }
  370. return result;
  371. };
  372. // Get the full name of the day for a date
  373. Date.prototype.getDayName = function() {
  374. return Date.dayNames[this.getDay()];
  375. };
  376. // Get the abbreviation of the day for a date
  377. Date.prototype.getDayAbbreviation = function() {
  378. return Date.dayAbbreviations[this.getDay()];
  379. };
  380. // Get the full name of the month for a date
  381. Date.prototype.getMonthName = function() {
  382. return Date.monthNames[this.getMonth()];
  383. };
  384. // Get the abbreviation of the month for a date
  385. Date.prototype.getMonthAbbreviation = function() {
  386. return Date.monthAbbreviations[this.getMonth()];
  387. };
  388. // Clear all time information in a date object
  389. Date.prototype.clearTime = function() {
  390. this.setHours(0);
  391. this.setMinutes(0);
  392. this.setSeconds(0);
  393. this.setMilliseconds(0);
  394. return this;
  395. };
  396. // Add an amount of time to a date. Negative numbers can be passed to subtract time.
  397. Date.prototype.add = function(interval, number) {
  398. if (typeof(interval)=="undefined" || interval==null || typeof(number)=="undefined" || number==null) {
  399. return this;
  400. }
  401. number = +number;
  402. if (interval=='y') { // year
  403. this.setFullYear(this.getFullYear()+number);
  404. }
  405. else if (interval=='M') { // Month
  406. this.setMonth(this.getMonth()+number);
  407. }
  408. else if (interval=='d') { // Day
  409. this.setDate(this.getDate()+number);
  410. }
  411. else if (interval=='w') { // Weekday
  412. var step = (number>0)?1:-1;
  413. while (number!=0) {
  414. this.add('d',step);
  415. while(this.getDay()==0 || this.getDay()==6) {
  416. this.add('d',step);
  417. }
  418. number -= step;
  419. }
  420. }
  421. else if (interval=='h') { // Hour
  422. this.setHours(this.getHours() + number);
  423. }
  424. else if (interval=='m') { // Minute
  425. this.setMinutes(this.getMinutes() + number);
  426. }
  427. else if (interval=='s') { // Second
  428. this.setSeconds(this.getSeconds() + number);
  429. }
  430. return this;
  431. };