Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question A Date Due Script

    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.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Bingo. Here ya go

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Western US
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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

  • #4
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you both

    I just want to thank you both for the help. Although I dont understand what you mean by "Pass the setNewDate function ..."
    Last edited by ekoms; 08-27-2002 at 11:18 PM.

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    Western US
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •