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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Comparing Dates in dynamic select box?

    I have been asked to write a script that will take an array of dates in mm/dd/yyyy format, write them into a select box (has to be done with javascript because its subject to change without reloading the current page), then auto select todays date, or the date closest to it without going beyond todays date.

    In the code below I have the date array hardcoded, it's actually passed back from a cold fusion query thats run in an iframe. This code works except its selecting tomorows date rather than yesterdays date when I pull out todays date.

    Thanks in advance...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    	<title>Untitled</title>
    <script language="JavaScript1.2">
    <!--
    dateArray = new Array("10/26/2002","10/27/2002","10/28/2002","10/29/2002","10/30/2002","10/31/2002","07/29/2002","07/28/2002","07/27/2002","07/26/2002","07/25/2002","07/24/2002","07/23/2002","07/22/2002","07/19/2002","07/18/2002","07/10/2002","07/09/2002","07/08/2002","07/05/2002","07/03/2002","07/02/2002","07/01/2002","06/26/2002","06/24/2002","06/20/2002","06/13/2002","06/10/2002","03/01/2002");
    
    function makeDates(){
    	myDate = new Date()
    	tMonth = myDate.getMonth()+1
    	if(tMonth <= 9){ curMonth = "0" + tMonth }else{ curMonth = tMonth }
    	if(myDate.getDate() <= 9){ curDay = "0" + myDate.getDate() }else{ curDay = myDate.getDate() }
    	curYear = myDate.getFullYear()
    	curDate = curMonth + "/" + curDay + "/" + curYear
    
    	for(i=0;i<dateArray.length;i++){
    	 document.test.dateRange.options[i] = null
    	}
    	for(i=0;i<dateArray.length;i++){
    		if(dateArray[i].substr(0,2) >= curMonth){
    			if(dateArray[i].substr(3,2) >= curDay){
    				if(dateArray[i].substr(6,4) >= curYear){
    					document.test.dateRange.options[i] = new Option(dateArray[i],dateArray[i],true,true);
    				}
    			}
    		} else {
    			document.test.dateRange.options[i] = new Option(dateArray[i],dateArray[i],false,false);
    		}
    	}
    }
    //-->
    </script>
    </head>
    
    <body>
    <div align="center">
    <form name="test">
    <select name="dateRange"></select>
    <br>
    <input type="button" value="makedates" onClick="makeDates()">
    <input type="button" value="ShowVal" onClick="alert(document.test.dateRange.options[document.test.dateRange.selectedIndex].text)"></form>
    </div>
    </body>
    </html>

  • #2
    New to the CF scene
    Join Date
    Jul 2002
    Location
    Oxford, England
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Hey!

    This is what I came up with:

    <html>
    <head>
    <title>Making Dates</title>
    <script language="JavaScript" type="text/JavaScript">
    var d=new Array("10/26/2002","10/27/2002","10/28/2002","10/29/2002","10/30/2002","10/31/2002","07/29/2002","07/28/2002","07/27/2002","07/26/2002","07/25/2002","07/24/2002","07/23/2002","07/22/2002","07/19/2002","07/18/2002","07/10/2002","07/09/2002","07/08/2002","07/05/2002","07/03/2002","07/02/2002","07/01/2002","06/26/2002","06/24/2002","06/20/2002","06/13/2002","06/10/2002","03/01/2002");

    function makeDates(){
    var t,c=new Date().getTime(),b=document["test"]["dateRange"].options
    for(var i=0;i<d.length;i++){b[i]=new Option(d[i])
    t=new Date().getTime()-new Date(d[i]).getTime()
    if(t<c&&t>=0){c=t;b[i].selected=true}
    }
    }
    </script>
    </head>

    <body>
    <FORM Name="test">
    <SELECT Name="dateRange"></SELECT>
    <input value="makedates" type="button" onClick="makeDates()">
    </FORM>
    </body>
    </html>


    Hope that helps,

    coxy

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks , thats what I needed it to do. Took me a bit to figure out what variables I needed to replace with mine but it works perfectly now. Thanks again.


  •  

    Posting Permissions

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