View Full Version : A Date Due Script

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.

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

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;
return workStr + toPad;
<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>


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 ..."

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.


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



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


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:
<title>Add to a date</title>

function adddate(formid)
alert("You must enter a number!")
adddays=parseInt(formid.tdays.value) // number of days to add or subtract
var newdate = new Date();
newdate.setDate(newdate.getDate() + adddays)

<body onLoad="document.myform.tdays.focus();document.myform.tdays.select()">
<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)">

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum