Hi I'm trying to do some basic javascript and am getting "native code" instead of what I want:
<script type="text/javascript">
var today = new Date();
document.write(today + "<br />");
//document.write(today.length + "<br />"); - was getting "undefined"
//document.write(today[0] + "<br />"); - was getting "undefined"
document.write(today.getMonth + "<br />");
document.write(today.getMonth + "<br />");
document.write(today.getFullYear + "<br />");
</script>
output was:
Fri Jan 13 14:13:01 EST 2012
function getMonth() { [native code] }
function getDay() { [native code] }
function getFullYear() { [native code] }
what I want is to get the current Month, Day, Year and put it into an array variable that I will be able to call later on. I'm not getting far because of this native code. Can someone tell me what it is and hopefully, more importantly I can complete this project? Thank you for your time and help, it is much appreciated!
-
For exercise purposes you could have your script within <PRE> and use document.writelnOnTheFly– OnTheFly2012年01月13日 21:15:21 +00:00Commented Jan 13, 2012 at 21:15
4 Answers 4
The getMonth and the rest are functions, not properties, when you call just today.getMonth you are getting a reference to the actual function. But, if you execute it using parenthesis, you get the actual result.
Your code should be:
document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
Comments
You are missing the parenthesis().
document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
Comments
document.write(today.getMonth() + "<br />"); // notice the ()'s to invoke the function
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
Comments
getMonth and getFullYear are functions, so you need to invoke them. Note the parentheses:
document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
As you have it, it's printing the string representations of the functions, not the functions' values.