Simplify Date code in JavaScript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
soundbwoy
Forum Newbie
Posts: 21
Joined: Sat Jun 07, 2003 5:22 pm
Location: Orlando, FL

Simplify Date code in JavaScript

Post 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>
Skyzyx
Forum Commoner
Posts: 42
Joined: Fri Feb 14, 2003 1:53 am
Location: San Jose, CA

Post 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).
Post Reply