...

View Full Version : A Date Due Script



ekoms
08-26-2002, 05:34 PM
I am fairly new at Javascript and was wondering if anyone could point me to a good reading source or a script itself, that allows you to display a date based on the current date. For example I work in a library so our materials have different check out times. Everything except for videos are checked out for 2 weeks, videos however are only for 2 days. I wrote a basic script that just added 14 and 2 to the current date, but the date does not roll over at the end of the month, Ex. Books checked out today 8-26-02 would be due on
9-9-02, but my script displays it as 8-40-02. Thank you ahead of time for any help I receive.

beetle
08-26-2002, 05:43 PM
Bingo. Here ya go (http://www.webreference.com/js/tips/010219.html)

ShriekForth
08-26-2002, 06:08 PM
This is how I handled that. This is stripped down, but it will do what you are asking for. Pass the setNewDate function the nuber of days you want to advance the date. If a date exists in the field, the days will be added to that if it can be parsed as a date. Otherwise it will add it to today's date. It's generic enough to modify to your needs.



<script type="text/javascript" language="JavaScript">
function setNewDate(daysToSpan){
if (!document.temp.newDays.value){
sysDate = new Date();
}
else {
sysDate = new Date(document.temp.newDays.value);
}
if(daysToSpan > 0){
gmtDate = new Date( daysToSpan * 86400000);
newDate = new Date(sysDate.getTime() + gmtDate.getTime());
}
document.temp.newDays.value = pad(newDate.getMonth() +1, '0', 2) + "/" + pad(newDate.getDate(), '0', 2) + "/" + newDate.getUTCFullYear();
}

function pad(toPad, padding, size){
ct = 0;
workStr = new String(toPad);
tlength = workStr.length;
workStr = new String("");
while(ct < (size - tlength)){
workStr = workStr + padding;
ct++;
}
return workStr + toPad;
}
</script>
</head>
<body>
<form name="temp">
<input type="text" name="newDays" value=""><br>
<input type="text" name="toSpan"><a href="javascript:setNewDate(document.temp.toSpan.value)">Move</a>
</form>

ShriekForth

ekoms
08-27-2002, 09:47 PM
I just want to thank you both for the help. Although I dont understand what you mean by "Pass the setNewDate function ..."

ShriekForth
08-28-2002, 04:10 PM
Just that when you call the setNewDate(*num*) function, it takes a parameter that is the value of the days to span. In the case of the example, it allows the user to enter a number that is read out of a field.

setNewDate(document.temp.toSpan.value)

It could just as easiy be hardcoded to a certain time , 14 days or so.

setNewDate(14)

ShriekForth

ekoms
08-28-2002, 05:49 PM
I must be really stupid because I can not get the script to run correctly. I didnt find the string "setNewDate(document.temp.toSpan.value)" in the code, so I told it that var daysToSpan = (14), but I dont understand the table either. I'm sorry for my ignorance, but I want to learn how to do this. Thank you for your time and replies

ekoms

requestcode
08-28-2002, 06:08 PM
I confuse you even more by giving you another example. This one will allow you to enter a number and have it add to or subtract from the current date.(to subtract enter a negative number - ie:-2) I did not format the date so you will get the default format. Here it is:
<html>
<head>
<title>Add to a date</title>
<SCRIPT LANGUAGE="JavaScript">

function adddate(formid)
{
if(isNaN(formid.tdays.value)||formid.tdays.value.length==0)
{
alert("You must enter a number!")
formid.tdays.focus()
formid.tdays.select()
}
else
{
adddays=parseInt(formid.tdays.value) // number of days to add or subtract
var newdate = new Date();
newdate.setDate(newdate.getDate() + adddays)
formid.tdate.value=newdate
}
}

</SCRIPT>
</head>
<body onLoad="document.myform.tdays.focus();document.myform.tdays.select()">
<BR><BR>
<FORM NAME="myform">
Enter the number of days to add or subtract to the current date: <INPUT TYPE="text" size="4" NAME="tdays" VALUE="0">
<INPUT TYPE="text" size="25" NAME="tdate">
<INPUT TYPE="button" VALUE="Get New Date" onClick="adddate(this.form)">
</FORM>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum