Raised This Month: $32 Target: $400
 8% 

Javascript Question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dizzy
Veteran Member
Join Date: Jun 2004
Location: Massachusetts
Old 03-24-2007 , 14:49   Javascript Question
Reply With Quote #1

Hey guys, I don't know what I'm doing wrong here

I want it to read [March 24th, 2007]

Here's my code:
Code:
<script type="text/javascript">
<!--
var d = new Date()
var month = d.getMonth() + 1
var day = d.getDate()
var year = d.getFullYear()
var vmonth = 0
var dayend = 0

//Start Of Month If Statements
if (month = 1)
{
  vmonth = January
}
if (month = 2)
{
  vmonth = February
}
if (month = 3)
{
  vmonth = March
}
if (month = 4)
{
  vmonth = April
}
if (month = 5)
{
  vmonth = May
}
if (month = 6)
{
  vmonth = June
}
if (month = 7)
{
  vmonth = July
}
if (month = 8)
{
  vmonth = August
}
if (month = 9)
{
  vmonth = September
}
if (month = 10)
{
  vmonth = October
}
if (month = 11)
{
  vmonth = November
}
if (month = 12)
{
  vmonth = December
}
//End Of Month If Statements

//Start Of DayEnd If Statements
if (day = 1 || 21 || 31)
{
  dayend = st
}
if (day = 2 || 22)
{
  dayend = nd
}
if (day = 3 || 23)
{
  dayend = rd
}
if (day = 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15 || 16 || 17 || 18 || 19 || 20 || 24 || 25 || 26 || 27 || 28 || 29 || 30)
{
  dayend = th
}
//End Of Month If Statements


document.write(vmonth + " " + day + dayend + ", " + year)
//-->
</script>
Dizzy is offline
Send a message via AIM to Dizzy
jRaven
AMX Mod X Beta Tester
Join Date: Jan 2006
Location: IA, USA
Old 03-24-2007 , 15:00   Re: Javascript Question
Reply With Quote #2

See commonbullet's post.

Last edited by jRaven; 03-25-2007 at 17:35. Reason: better code
jRaven is offline
Send a message via ICQ to jRaven Send a message via AIM to jRaven
commonbullet
Veteran Member
Join Date: Oct 2005
Old 03-24-2007 , 15:34   Re: Javascript Question
Reply With Quote #3

For all comparisons you should use ==; i.e.
(month == 2) ..,

Also this has not the behavior you expect
(day == 1 || 21)
The correct is
(day == 1 || day == 21)


I don't remember if there is a native to pick up month names in javascript, but your code could be simpler if you create an array of months like that.
Code:
var monthNames = new Array("January", "February", "March", 
			   "April", "May", "June", "July",
			   "August", "September", "October",
			   "November", "December");
And then
Code:
document.write(monthNames[month] + " " + day + dayend + ", " + year);
In this case you wouldn't need to add 1 to month

Also you could make it very cleaner by using 'switches'.
Code:
switch(day)
{
	case 1:
	case 21:
	case 31:
		dayend = "st";
		break;
	case 2:
	case 22:
		dayend = "nd";
		break;
	case 3:
	case 23:
		dayend = "rd";
		break;
	default:
		dayend = "th";
}

Last edited by commonbullet; 03-24-2007 at 15:44.
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
Dizzy
Veteran Member
Join Date: Jun 2004
Location: Massachusetts
Old 03-24-2007 , 23:12   Re: Javascript Question
Reply With Quote #4

Wow CommonBullet, thanks, soo much useful information in your post...

I'll try to make a neat code ;)

I'll get back to you
Dizzy is offline
Send a message via AIM to Dizzy
Dizzy
Veteran Member
Join Date: Jun 2004
Location: Massachusetts
Old 03-24-2007 , 23:18   Re: Javascript Question
Reply With Quote #5

Here's the final code, and it works

Code:
<script type="text/javascript">
<!--
var d = new Date()
var month = d.getMonth() + 1
var day = d.getDate()
var year = d.getFullYear()
var vmonth = 0
var dayend = 0

if (month == 1)
{
  vmonth = "January"
}
if (month == 2)
{
  vmonth = "February"
}
if (month == 3)
{
  vmonth = "March"
}
if (month == 4)
{
  vmonth = "April"
}
if (month == 5)
{
  vmonth = "May"
}
if (month == 6)
{
  vmonth = "June"
}
if (month == 7)
{
  vmonth = "July"
}
if (month == 8)
{
  vmonth = "August"
}
if (month == 9)
{
  vmonth = "September"
}
if (month == 10)
{
  vmonth = "October"
}
if (month == 11)
{
  vmonth = "November"
}
if (month == 12)
{
  vmonth = "December"
}

switch(day)
{
	case 1:
	case 21:
	case 31:
		dayend = "st";
		break;
	case 2:
	case 22:
		dayend = "nd";
		break;
	case 3:
	case 23:
		dayend = "rd";
		break;
	default:
		dayend = "th";
}


document.write(vmonth + " " + day + dayend + ", " + year)
//-->
</script>
If I get that native for the month names, I'll make it even neater
Dizzy is offline
Send a message via AIM to Dizzy
Lord_Destros
Veteran Member
Join Date: Jul 2004
Location: With stupid.
Old 03-25-2007 , 00:10   Re: Javascript Question
Reply With Quote #6

If you don't use common bullet's suggestion for using an array for month names, you should at least use another switch.
__________________
Quote:
Originally Posted by Twilight Suzuka
Don't worry m'lord. The turtles day will come.
Lord_Destros is offline
Send a message via AIM to Lord_Destros
Dizzy
Veteran Member
Join Date: Jun 2004
Location: Massachusetts
Old 03-25-2007 , 00:57   Re: Javascript Question
Reply With Quote #7

Here's my final copy including a time code that automatically refreshes

Code:
<center><script type="text/javascript">
<!--
var d = new Date();

var r_month = 0;

var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();
var dayend = 0;

switch(month)
{
  case 1:
    r_month = "January";
    break;
  case 2:
    r_month = "February";
    break;
  case 3:
    r_month = "March";
    break;
  case 4:
    r_month = "April";
    break;
  case 5:
    r_month = "May";
    break;
  case 6:
    r_month = "June";
    break;
  case 7:
    r_month = "July";
    break;
  case 8:
    r_month = "August";
    break;
  case 9:
    r_month = "September";
    break;
  case 10:
    r_month = "October";
    break;
  case 11:
    r_month = "November";
    break;
  case 12:
    r_month = "December";
    break;
}

switch(day)
{
  case 1:
  case 21:
  case 31:
    dayend = "st";
    break;
  case 2:
  case 22:
    dayend = "nd";
    break;
  case 3:
  case 23:
    dayend = "rd";
    break;
  default:
    dayend = "th";
    break;
}

document.write(r_month + " " + day + dayend + ", " + year);
//-->
</script><br>
<div id="js_clock">
<script language="javascript">
function js_clock()
{
  var clock_time = new Date();
  var clock_hours = clock_time.getHours();
  var clock_minutes = clock_time.getMinutes();
  var clock_seconds = clock_time.getSeconds();
  var clock_suffix = "AM";
  if (clock_hours > 11)
  {
    clock_suffix = "PM";
    clock_hours = clock_hours - 12;
  }
  if (clock_hours == 0)
  {
    clock_hours = 12;
  }
  if (clock_hours < 10)
  {
    clock_hours = "0" + clock_hours;
  }
  if (clock_minutes < 10)
  {
    clock_minutes = "0" + clock_minutes;
  }
  if (clock_seconds < 10)
  {
    clock_seconds = "0" + clock_seconds;
  }
  var clock_div = document.getElementById('js_clock');
  clock_div.innerHTML = clock_hours + ":" + clock_minutes + ":" + clock_seconds + " " + clock_suffix;setTimeout("js_clock()", 1000);
}
js_clock();
</script>
</div>
</center>
If you see any redundant code or ways to cut lines just repost

Trying to make the cleanest code possible.

Even renaming my variables so it's more practical.

Anything to make it cleaner Thanks in advance!

If you want to see the code in action just visit the site I made it for

http://remembrance.1gb.cc

It's under that affiliates section on the left

Thanks guys!

Last edited by Dizzy; 03-25-2007 at 03:19.
Dizzy is offline
Send a message via AIM to Dizzy
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 03-25-2007 , 11:55   Re: Javascript Question
Reply With Quote #8

Code:
  if (clock_hours == 0)
  {
    clock_hours = 12;
  }
  if (clock_hours < 10)
  {
    clock_hours = "0" + clock_hours;
  }
If the first is true, the second will obviously be false and so won't need to be checked. Nothing significant.

Code:
if (clock_hours == 0)
{
    clock_hours = 12;
}
else if (clock_hours < 10)
{    
    clock_hours = "0" + clock_hours;
}
IIRC you don't need to break from the last case statement in a switch. No idea whether removing it would alter the final executable in any way.

Last edited by Lee; 03-25-2007 at 12:02.
Lee is offline
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 03-25-2007 , 12:34   Re: Javascript Question
Reply With Quote #9

Taking what commonbullet said, the first script could be simplified to.
Code:
<script type="text/javascript">
<!--
var monthNames = new Array("January", "February", "March", 
			   "April", "May", "June", "July",
			   "August", "September", "October",
			   "November", "December");

var d = new Date();

var month = d.getMonth();
var day = d.getDate();
var year = d.getFullYear();
var dayend = 0;
var r_month = monthNames[month];

switch(day)
{
  case 1:
  case 21:
  case 31:
    dayend = "st";
    break;
  case 2:
  case 22:
    dayend = "nd";
    break;
  case 3:
  case 23:
    dayend = "rd";
    break;
  default:
    dayend = "th";
    break;
}

document.write(r_month + " " + day + dayend + ", " + year);
//-->
</script>
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode