Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying the date

Status
Not open for further replies.

MONFU

Programmer
Oct 27, 2001
35
MT
Dear all, I have created a function to display today's date in mm/dd/yyyy format and now I wish to display it in the form as soon as the user enters, so by default, if he does not want to change it, there is always today's date displayed.

The function is as follows:-
function getTodaysDate() {

var today = new Date()
var todayDate = today.getDate()
var todayMonth = today.getMonth() + 1
var todayYear = today.getYear()
if (todayMonth < 10)
{
todayMonth = ( &quot;0&quot; + todayMonth)
}

var fulldate = ( todayMonth + &quot;/&quot; + todayDate + &quot;/&quot; + todayYear )
return fulldate
}

Now I wish to display it in the form, ie:-

<input type=&quot;text&quot; name=&quot;insertDate&quot; value=&quot;????&quot; size=&quot;13&quot; maxlength=&quot;10&quot;

How can I put the function in the value field?

Thanks for your help
 
Modify your code to this:

function getTodaysDate() {
.
.
.
var fulldate = ( todayMonth + &quot;/&quot; + todayDate + &quot;/&quot; + todayYear );

document.formName.insertDate.value = fulldate; //change &quot;formName&quot; to the real name
}

Then add onLoad evebt handler to the <body> tag:
<body ... onload=&quot;getTodaysDate()&quot;>

 
Also, it's wise to use:

getFullYear();

or you'll end up with a year of 102 on many computers.
 
Hello Starway again,

Thanks for helping me out but the code did not work. I amended it as you said :-

<script>
function getTodaysDate() {

var today = new Date()
var todayDate = today.getDate()
var todayMonth = today.getMonth() + 1
var todayYear = today.getYear()
if (todayMonth < 10)
{
todayMonth = ( &quot;0&quot; + todayMonth)
}

var fulldate = ( todayMonth + &quot;/&quot; + todayDate + &quot;/&quot; + todayYear )
document.changeNews.insertDate.value = fulldate;
}

</script>

and then I tried to display it in the form like this:-

<p>
<input type=&quot;text&quot; name=&quot;insertDate&quot; onload=&quot;getTodaysDate()&quot; size=&quot;13&quot; maxlength=&quot;10&quot;
onchange=validate_date(changeNews.insertDate)>
</p>

Do I have something wrong?

Thanks for your help
 
What is:

validate_date(changeNews.insertDate)

and why is it not in quotes in the onchange event handler?

And why are you putting an onload statement in an input instead of in the body statement?
 
What is:

validate_date(changeNews.insertDate)

and why is it not in quotes in the onchange event handler?

And why are you putting an onload in an input instead of in the body statement?
 
Upppsss, my mistake was that I was not putting the onLoad method in the body but in the place of the value tag. Now it works.

Thanks very much for your help!
 
Now I am trying to do it as a global function so that I can access it from many ASP pages from within the same code.

I created this function
function getTodaysDate(formName) {

var today = new Date()
var todayDate = today.getDate()
var todayMonth = today.getMonth() + 1
var todayYear = today.getYear()
if (todayMonth < 10)
{
todayMonth = ( &quot;0&quot; + todayMonth)
}

var fulldate = ( todayMonth + &quot;/&quot; + todayDate + &quot;/&quot; + todayYear )
document.formName.value = fulldate;
}

Then in the body I am doing the following code
<body …. onLoad=&quot;getTodaysDate(changeNews.insertDate)&quot;>

But I have errors in the code, it is telling me that formName is null. Can you please help me out?

Thanks veru much
 
You need to look closely at what the parameter is that you send to the function in the onload statement, and how you refer to it in the function. You can't directly access a form element with document without using the form name as well.

If you use:

<body …. onLoad=&quot;getTodaysDate(document.changeNews.insertDate)&quot;>

then in the function (though referring to an element as formname is confusing):

formName.value = fulldate;

it should work.

And, like I wrote previously, if you don't use getFullYear(), some people are going to have the year 102 on their computer for the year number.


 
Ok trollacious,

It worked this way thanks. I also changed getYear() to getFullYear() but the problem is while getYear() was being highlighted in cyan (to show me that it is a method, getFullYear() is not. Do I have to specify something in the script?

Thanks again for your help!
 
According to Microsoft's help files:

Description
Returns the year stored in the Date object according to local time.

Syntax
objDate.getFullYear()

[/b]Remarks[/b]
To get the year according to Universal Coordinated Time (UTC), use the getUTCFullYear method.

The getFullYear method returns the year as an absolute number. For example, the year 1976 is returned as 1976. This avoids problems with dates occuring at the end of the 20th century.


--------------------------------------------------------------------------------


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top