cancel
Showing results for 
Search instead for 
Did you mean: 

Error on GetMonth() object

Former Member
0 Kudos

Hi SDN,

I am new to Jscript. The following code gives me an error saying that getmonth() is not a valid method on 'Today.'

Please help.

Thanks.

Saf.

-


HTML>

<HEAD>

<script language="javascript">

function get_quarter(){

var today = new Date()

var this_month = 0

var this_year = 0

this_month = today.getmonth()

this_year = today.getyear()

switch(this_month){

case 0,1,2:

return this_year+'1'

case 3,4,5:

return this_year+'2'

case 6,7,8:

return this_year+'3'

case 9,10,11:

return this_year+'4'

}

function say_it(){

document.write("The current quarter is " + get_quarter());

}

</script>

</HEAD>

<BODY>

<script language="JavaScript">

say_it();

</script>

</BODY>

</HTML>

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Safdar,

Jscript is case sensitive so get<b>m</b>onth() and get<b>M</b>onth() are different, getmonth() is not a method of object Date, getMonth() instead is.

The same applies to getYear() instead of getyear();

Said that, a few notes:

1) in jscript you do not need to initialize your variables so you can simply write:

var this_month = today.getMonth();

2) if you need a 4 digit year, eg: 2006 use method getFullYear() instead of getYear()

3) getMonth() returns a string, if you need an int, as for the if block you can use parseInt function;

4) as far as i remember (don't have a reference manual in my hands) you can't use switch with multiple conditions (0,1,2) you need an if for this

5) as jscript is a scripting language next time you have problem i suggest you to have a look at

, maybe you'll get a faster response.

This is your code reviewed:

function get_quarter(){

var today = new Date();

var this_month = today.getMonth();

var this_year = today.getYear();

var quarter;

var intMonth = parseInt(this_month);

if(intMonth >= 0 && intMonth < 3){

quarter = this_year+'1';

}else if(intMonth >= 3 && intMonth < 6){

quarter = this_year+'2';

}else if(intMonth >= 6 && intMonth < 9){

quarter = this_year+'3';

}else{

quarter = this_year+'4';

}

return quarter;

}

Hope it helps!

Kind Regards,

Sergio

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Safdar,

the best and fastest way to get help with javascript is the net. Use google and in 99 cases out of a 100 you will find what you're looking for a lot faster than anyone will reply in a forum.

For instance, searching with the keywords "get" "month" "date" "javascript" will yield this result (amongs thousands)

<a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">link</a>

regards,

Dion