Page 1 of 1

Simplify Date code in JavaScript

Posted: Sat Jun 14, 2003 1:38 pm
by soundbwoy
Can anyone help me make this code more efficient. I don't want to use the Arrays to get the month & weekdays, how can I get the Month and weekdays from the date function?

Code: Select all

script language="JavaScript" type="text/JavaScript"> 
<!-- 
daysofweek   = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
monthsofyear = new Array('January','February','March','April','May','June','July','August'_ 
,'September','October','November','December'); 

d=new Date(); 
weekDayName = d.getUTCDay(); 
theMonth = d.getMonth(); 

document.write ("<img src="images/date/wd_"+daysofweek&#1111;weekDayName]+".gif">"); 
document.write ("<img src="images/date/"+monthsofyear&#1111;theMonth]+".gif">"); 
document.write ("<img src="images/date/"+d.getUTCDate()+".gif">"); 
document.write ("<img src="images/date/yr_"+d.getYear()+".gif">"); 
//--> 
</script>

Posted: Tue Jul 01, 2003 1:58 am
by Skyzyx
In JavaScript, dateObj.getMonth() will return a value of 0-11 (Jan-Dec). In order for that to translate into "January" or "July", you need to have it read from an array. The same is true for the Weekdays (being 0-6).