views:

30

answers:

3

i finished it

<script> 
Number.prototype.pad = function(n) { 
for(var r = this.toString(); r.length < n; r = 0 + r); 
return r;  
}; 

function getThirdMondayInJanuary(year) { 
var d = new Date(); 
 if (year) d.setFullYear(year); 
 d.setMonth(0,15,0,0,0,0); // 15th of Jan 
 d.setDate(d.getDate()+([1,0,6,5,4,3,2][d.getDay()])); //get next Monday
return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
} 

function getThirdMondayInFebruary(year) { 
var d = new Date(); 
 if (year) d.setFullYear(year); 
d.setMonth(1,15,0,0,0,0); // 15th of Feb 
 d.setDate(d.getDate()+([1,0,6,5,4,3,2][d.getDay()])); //get next Monday
return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
} 

function getLastFridayInApril(year) { 
 var d = new Date(); 
 if (year) d.setFullYear(year); 
 d.setMonth(3,25,0,0,0,0); // 25th of April 
 d.setDate(d.getDate()+([5,4,3,2,1,0,6][d.getDay()])); //get next Friday
 return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
 } 

 function getLastMondayInMay(year) { 
 var d = new Date(); 
 if (year) d.setFullYear(year); 
 d.setMonth(4,25,0,0,0,0); // 20th of May 
 d.setDate(d.getDate()+([1,0,6,5,4,3,2][d.getDay()])); //get next Monday
 return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
 }  
 function getFirstMondayInSept(year) { 
 var d = new Date(); 
 if (year) d.setFullYear(year); 
 d.setMonth(8,1,0,0,0,0); // 1st of Sep 
 d.setDate(d.getDate()+([1,0,6,5,4,3,2][d.getDay()])); //get next Monday
 return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
 }  
 function getSecondMondayInOct(year) { 
 var d = new Date(); 
 if (year) d.setFullYear(year); 
 d.setMonth(9,7,0,0,0,0); // 7st of Oct 
 d.setDate(d.getDate()+([1,0,6,5,4,3,2][d.getDay()])); //get next Monday
 return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
 }  
 function getFourthThursdayInNov(year) { 
 var d = new Date(); 
 if (year) d.setFullYear(year); 
 d.setMonth(10,20,0,0,0,0); // 20th of Nov 
 d.setDate(d.getDate()+([4,3,2,1,0,6,5][d.getDay()])); //get next Thursday
 return (d.getMonth()+1).pad(2)+'/'+d.getDate().pad(2); 
 }
 var availableTags=["New years Day", "Martin Luther King Day", "Groundhog Day", "Valentine's Day", "Washington's Birthday", 
  "Easter", "Earth Day", "National Arbor Day", "Mother's Day", "Memorial Day", "Flag Day", "Father's Day", "Independence Day", 
  "Labor Day", "Columbus Day", "Halloween", "Veterans Day", "Thanksgiving Day", "Pearl Harbor Remembrance Day", "Christmas Day"] 
  $(document).ready(function() { 
 $('#holidayDate').datepicker(); 
 $("#tags").autocomplete({ 
 source:availableTags, 
 select:function(event,ui) { 
 var res = ""; 
 switch (ui.item.value) { 
   case availableTags[0]: res = '01/01'; break; 
   case availableTags[1]: res = getThirdMondayInJanuary(); break;    
   case availableTags[2]: res = '02/02'; break; 
   case availableTags[3]: res = '02/14'; break; 
   case availableTags[4]: res = getThirdMondayInFebruary(); break;           
   case availableTags[6]: res = '04/22'; break;
   case availableTags[7]: res = getLastFridayInApril(); break;
   case availableTags[9]: res = getLastMondayInMay();     break;                         
   case availableTags[10]: res = '06/14'; break;        
   case availableTags[12]: res = '07/04'; break; 
   case availableTags[13]: res = getFirstMondayInSept(); break; 
   case availableTags[14]: res = getSecondMondayInOct(); break;                
   case availableTags[15]: res = '10/31'; break; 
   case availableTags[16]: res = '11/11'; break; 
   case availableTags[17]: res = getFourthThursdayInNov(); break;                     
   case availableTags[18]: res = '12/07'; break; 
   case availableTags[19]: res = '12/25'; break; 
   } 
    if (res) res += '/'+new Date().getFullYear(); 
   $('#holidayDate').val(res); 
   } 
   }); 
   $('#holidayDate').change(function() { 
   var dateString = $(this).val().substring(0, 5); 
   var year = $(this).val().substring(6); 
   var res = ""; 
   switch (dateString) { 
   case '01/01': res = availableTags[0]; break; 
   case getThirdMondayInJanuary(): res = availableTags[1]; break; 
   case '02/02': res = availableTags[2]; break; 
   case '02/14': res = availableTags[3]; break; 
   case getThirdMondayInFebruary(): res = availableTags[4]; break;        
   case '04/22': res = availableTags[6]; break; 
   case getLastFridayInApril(): res = availableTags[7]; break;       
   case getLastMondayInMay(): res = availableTags[9]; break;              
   case '06/14': res = availableTags[10]; break; 
   case '07/04': res = availableTags[12]; break;
   case getFirstMondayInSept(): res = availableTags[13]; break; 
   case getSecondMondayInOct(): res = availableTags[14]; break;                            
   case '10/31': res = availableTags[15]; break; 
   case '11/11': res = availableTags[16]; break; 
   case getFourthThursdayInNov(): res = availableTags[17]; break;        
   case '12/07': res = availableTags[18]; break; 
   case '12/25': res = availableTags[19]; break;
 } 
 $('#tags').val(res); 
 }); 

 });  

 </script>    
+1  A: 

You are using a reserved word (Date).

Set it to something like

var myDate = [Date().january().third().monday(),Date().february().third().monday()];
Marko
A: 

You can't use the builtin Date object when you declare a variable called Date. Rename your variable to something else.

Jani Hartikainen
A: 

As mentioned Date is a reserved word so can't be used for a variable name.

Also far as I know

Date().january().third().monday()

is not valid code. Came up with this function - see http://jsfiddle.net/TNhpL/1/ - to return a specified day for a month.

Castrohenge