Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,533 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,170 people online right now. Registration is fast and FREE... Join Now!




Help with a Date Variable ina webpage

 
Reply to this topicStart new topic

Help with a Date Variable ina webpage

DarthSideous
post 9 Oct, 2008 - 01:14 PM
Post #1


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 7


My Contributions


I am sure I am missing the trees from the forest here but I am really in need of some guidance. I have the following script that I am modifying
to work within a webpage in order to log dated activities. I can print the date variables via "document.write" but for some reason can not seem to
get the variable to show up in the textboxes.

Any help would be greatly appreciated

CODE


<html>
<head>

<meta http-equiv="Content-Language" content="en-us">

<script type="text/javascript" language="JavaScript">

function getMonthDate()
{
   var months = new Array(13);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthString = monthnumber + 1;
                  
   return monthString;
} // function getMonthDate()

function getDayDate()
{
   var now         = new Date();  
   var monthday    = now.getDate();
   var DayString = monthday;
                  
   return DayString;
} // function getDayDate()

function getYearDate()
{
   var now         = new Date();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var YearString = year;
        
   return YearString;
} // function getDayDate()

</script>

</head>
<body>
<script>

var monthnumber = getMonthDate();
var monthday = getDayDate();
var year = getYearDate();
// Show the variables are moving thru the script
document.write('Date is ');
document.write('<br>');
document.write(monthnumber);
document.write('<br>');
document.write(monthday);
document.write('<br>');
document.write(year);
document.write('<br>');

</script>

//EVERYTHING APPEARS TO WORK FINE TO THIS POINT

<p><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Year<br>

Today's Date:

<input type=text name=monthnumber size=6>
<input type=text name=monthday size=6>
<input type=text name=year size=6>

</p>

</body>
</html>
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 9 Oct, 2008 - 02:18 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


Here you are, you need to use Ids and make sure to make calls to the ids after the elements have been loaded (either by placing the code at the bottom of the page, or by adding an onload function, I put the code at the bottom of the page).

CODE
<script type="text/javascript" language="JavaScript">

function getMonthDate(){
   var months = new Array(12);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthString = monthnumber + 1;
                  
   return monthString;
}

function getDayDate(){
   var now         = new Date();  
   var monthday    = now.getDate();
   var DayString = monthday;
                  
   return DayString;
}

function getYearDate(){
   var now         = new Date();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var YearString = year;
        
   return YearString;
}

var monthnumber = getMonthDate();
var monthday = getDayDate();
var year = getYearDate();



// Show the variables are moving thru the script
document.write('Date is ');
document.write('<br>');
document.write(monthnumber);
document.write('<br>');
document.write(monthday);
document.write('<br>');
document.write(year);
document.write('<br>');

</script>

Today's Date:

<input id="t" type="text" name="monthnumber" size="6">
<input id="tt" type="text" name="monthday" size="6">
<input id="ttt" type="text" name="year" size="6">

<script>
document.getElementById("t").value = monthnumber;
document.getElementById("tt").value = monthday;
document.getElementById("ttt").value = year;
</script>


I also went through and got rid of some of the extra stuff you had that wasn't needed for the script to work.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

DarthSideous
post 9 Oct, 2008 - 04:51 PM
Post #3


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 7


My Contributions


BetaWar

Thanks...that is great Obviously I am learn JS.... What is the purpose of GetElementByID? Does this just allow a .value property to be assigned to the document variable or is there some other reason to have to take that step?

Again thatnks for your efforts...Bigtime help



QUOTE(BetaWar @ 9 Oct, 2008 - 03:18 PM) *

Here you are, you need to use Ids and make sure to make calls to the ids after the elements have been loaded (either by placing the code at the bottom of the page, or by adding an onload function, I put the code at the bottom of the page).

CODE
<script type="text/javascript" language="JavaScript">

function getMonthDate(){
   var months = new Array(12);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthString = monthnumber + 1;
                  
   return monthString;
}

function getDayDate(){
   var now         = new Date();  
   var monthday    = now.getDate();
   var DayString = monthday;
                  
   return DayString;
}

function getYearDate(){
   var now         = new Date();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var YearString = year;
        
   return YearString;
}

var monthnumber = getMonthDate();
var monthday = getDayDate();
var year = getYearDate();



// Show the variables are moving thru the script
document.write('Date is ');
document.write('<br>');
document.write(monthnumber);
document.write('<br>');
document.write(monthday);
document.write('<br>');
document.write(year);
document.write('<br>');

</script>

Today's Date:

<input id="t" type="text" name="monthnumber" size="6">
<input id="tt" type="text" name="monthday" size="6">
<input id="ttt" type="text" name="year" size="6">

<script>
document.getElementById("t").value = monthnumber;
document.getElementById("tt").value = monthday;
document.getElementById("ttt").value = year;
</script>


I also went through and got rid of some of the extra stuff you had that wasn't needed for the script to work.

Hope that helps.

User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 9 Oct, 2008 - 05:14 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


getElementById() returns a pointer to the object on the page that has that ID. This will allow you to use/ modify all the properties of the object who's pointer was returned.

It is a little like a lower level programming language with objects.

The <input> tags have the properties of somethings like:
CODE
value
type
name
id
className
and so on...


The string you input is the ID you are wanting to match with.

All that script is doing is getting a pointer to the text inputs and then setting their values.

Hope that makes sense.
User is offlineProfile CardPM

Go to the top of the page

DarthSideous
post 10 Oct, 2008 - 11:14 AM
Post #5


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 7


My Contributions


Yeah it does....

Again thanks for the guidance....



QUOTE(BetaWar @ 9 Oct, 2008 - 06:14 PM) *

getElementById() returns a pointer to the object on the page that has that ID. This will allow you to use/ modify all the properties of the object who's pointer was returned.

It is a little like a lower level programming language with objects.

The <input> tags have the properties of somethings like:
CODE
value
type
name
id
className
and so on...


The string you input is the ID you are wanting to match with.

All that script is doing is getting a pointer to the text inputs and then setting their values.

Hope that makes sense.

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 01:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month