Forum Moderators: open

Message Too Old, No Replies

Textbox and Default date range

         

scoobydoo987

4:20 am on Jul 31, 2005 (gmt 0)

10+ Year Member



How would I go about writing JavaScript code to show current date -30 on page_load in a textbox (BeginDate) and in another text box (EndDate) show the current date?

This is to be used as a date range.

Thanks.

scoobydoo987

5:41 am on Jul 31, 2005 (gmt 0)

10+ Year Member



I got the dates to write to the page but I still need the textboxes to be populated.

What do I need to modify in the code below to allow the BeginDate and EndDate textboxes to be populated with the date range upon page load?

<body onload="javascript:BeginDate(); EndDate();">
<SCRIPT LANGUAGE="JavaScript">
<!---
function BeginDate() {
current_date = new Date();
current_month = current_date.getMonth();
current_month = current_month + 1;
current_day = current_date.getDate();
current_year = current_date.getYear();
document.write("" + current_month + "/" + current_day + "/" + current_year);
document.write("<br>")
}

function EndDate() {
current_date = new Date();
current_month = current_date.getMonth();
current_month = current_month + 1;
current_day = current_date.getDate()-30;
current_year = current_date.getYear();
document.write("" + current_month + "/" + current_day + "/" + current_year);
document.write("<br>")
}
//-->
</SCRIPT>

<p><input type="text" name="BeginDate" size="20"></p>
<p><input type="text" name="EndDate" size="20"></p>

</body>

garann

8:26 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



You should be able to do it with just a couple of changes:

<SCRIPT LANGUAGE="JavaScript">
<!---
function BeginDate() {
current_date = new Date();
current_month = current_date.getMonth();
current_month = current_month + 1;
current_day = current_date.getDate();
current_year = current_date.getYear();
var fulldate = "" + current_month + "/" + current_day + "/" + current_year;
document.write(fulldate);

document.write("<br>")
document.getElementById("BeginDate").value = fulldate;
}

function EndDate() {
current_date = new Date();
current_month = current_date.getMonth();
current_month = current_month + 1;
current_day = current_date.getDate()-30;
current_year = current_date.getYear();
var fulldate = "" + current_month + "/" + current_day + "/" + current_year;
document.write(fulldate);

document.write("<br>")
document.getElementById("EndDate").value = fulldate;
}
//-->
</SCRIPT>

<p><input type="text" id="BeginDate" name="BeginDate" size="20"></p>
<p><input type="text" id="EndDate" name="EndDate" size="20"></p>

scoobydoo987

4:38 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



thanks garann. didn't quite work though. i added this to the body onload="javascript:BeginDate(), EndDate()" and instead of it populating the textboxes it only displays the fulldate as a label and no other fields or anything on the page.

any ideas?

garann

4:43 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



Oh, I didn't see that part... How about if you change your function names to dateBegin() and dateEnd(), and your body tag to
<body onload="dateBegin();dateEnd();">
?

scoobydoo987

5:28 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



It works but i don't quite understand how to get the BeginDate to be 30 days ago?

In vb I can do this:
txtBeginDate.Text = DateAdd("d", -30, Date.Now).ToShortDateString

But how do you do it in javascript?