7

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!

asked Jan 13, 2012 at 19:29
1
  • For exercise purposes you could have your script within <PRE> and use document.writeln Commented Jan 13, 2012 at 21:15

4 Answers 4

16

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 />");
answered Jan 13, 2012 at 19:32
Sign up to request clarification or add additional context in comments.

Comments

3

You are missing the parenthesis().

document.write(today.getMonth() + "<br />");
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
answered Jan 13, 2012 at 19:31

Comments

1
document.write(today.getMonth() + "<br />"); // notice the ()'s to invoke the function
document.write(today.getMonth() + "<br />");
document.write(today.getFullYear() + "<br />");
answered Jan 13, 2012 at 19:30

Comments

1

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.

answered Jan 13, 2012 at 19:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.