Web Hosting Talk







View Full Version : What's wrong with Javascript's getMonth()?


Qacer
07-29-2002, 12:31 PM
Hi all,

My client just alerted me of a date mistake. For some reason, getMonth() always displays the month before. Is this a bug or has it always been like this? Try the following code:

<html>
<head>
<title>test</title>

<script language="JavaScript">

function testdate()
{
var testx;

now = new Date();

testx = now.getMonth();
testy = "hello";

alert(testx);

};

</script>

</head>

<body onLoad="testdate();">

</body>

</html>

Lagniappe-labgeek
07-29-2002, 12:45 PM
This follows the Java convention - months will be 0 through 11

http://java.sun.com/products/jdk/1.1/docs/api/java.util.Date.html#getMonth()

getMonth
public int getMonth()

Note: getMonth() is deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

Returns the month represented by this date. The value returned is between 0 and 11, with the value 0 representing January.

Returns:
the month represented by this date.
See Also:
Calendar

Qacer
07-29-2002, 01:20 PM
Ahh.. I forgot about that whole date thing starting at 0. I've made the same mistake before in my scripts. By the way, do you happen to know of a way to check if all strings are uppercase or lowercase in Javascript? I've been searching all over the place, but found nothing so far. Thanks.

ckpeter
07-29-2002, 01:33 PM
You can try to convert the String to all upper/lower case, and then compare to see if that new string is equal to the old string (if so, then it was already in all upper/lower case).

I am only a java programmer, so I only know Java's String class can convert to upper/lower case. You can either check to see if javascript has such a function, or use the Java version via liveconnect.

Peter