Help with check date and hours

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    #1

    Help with check date and hours

    Help with check date and hours

    Hi all.

    I have this form:

    Code:
    <html>
    
    <head>
    
    </head>
    
    <body>
    
    <form name="orario" method="post" action="">
    
    <input type="text" name="hour" size="20">
    
    <input type="text" name="dates" size="20">
    
    <input type="submit" value="Invia" name="B1">
    
    </form>
    </body>
    
    </html>
    I need one function javascript check date and time insert in the form.

    If date is same the date now and the hour is less two hours the hours now, the form is stopped.

    For example:

    date now = 22/09/2008
    hours now = 14:50

    date = 22/09/2008
    hours = 15:30 ===> KO

    date = 22/09/2008
    hours = 16:49 ===> KO

    date = 22/09/2008
    hours = 16:50 ===> OK

    Can you help me?
    kind regards
    viki
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    hi Viki, can u please explain ur requirement clearly, if the time is lesser than the current time what should be done. I am not getting that point. can u please post it clearly.

    Regards
    Ramanan Kalirajan

    Comment

    • viki1967
      Contributor
      • Oct 2007
      • 263

      #3
      Originally posted by RamananKaliraja n
      hi Viki, can u please explain ur requirement clearly, if the time is lesser than the current time what should be done. I am not getting that point. can u please post it clearly.

      Regards
      Ramanan Kalirajan
      Hi thanks x your answer.

      I check this:

      * Date register is same at today;
      * Hours register is less two hours the now

      Do you understand?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Create a new Date object which will default to the current date and time. Then you can compare by creating another Date object using the input.

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Hi acoder; one example please?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            This link should help get you started.

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Originally posted by acoder
              This link should help get you started.
              No, I dont no... I dont understand your suggestion...

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Which part do you not understand? It shows how to create a Date object, it shows how you can set a date, and shows how to compare. OK, since you want to compare time, you will also need to set the time which you can do with setHours/setMinutes/setSeconds - see a full reference.

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #9
                  Originally posted by acoder
                  Which part do you not understand? It shows how to create a Date object, it shows how you can set a date, and shows how to compare. OK, since you want to compare time, you will also need to set the time which you can do with setHours/setMinutes/setSeconds - see a full reference.

                  OK, I try this but not working... :

                  Code:
                  <script language="javascript">
                  <!--
                  
                  function controllo_data(stringa){
                      	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
                      	  if (!espressione.test(stringa))
                      	  {
                          	return false;
                      	  }
                  	  else{
                               anno = parseInt(stringa.substr(6),10);
                               mese = parseInt(stringa.substr(3, 2),10);
                               giorno = parseInt(stringa.substr(0, 2),10);
                          
                  	     var data=new Date(anno, mese-1, giorno);
                                 if(data.getFullYear()==anno && data.getMonth()+1==mese && data.getDate()==giorno){
                              		return true;
                          		}
                  	       else{
                                   return false;
                                 }
                      	   }
                  }
                  	
                  function confronta_data(data1)
                  
                  {
                  	dataCorr = new Date();
                  	giorno = dataCorr.getDate();
                  	mese = dataCorr.getMonth();
                  	anno = dataCorr.getYear();
                  	data = anno+mese+giorno;
                  	
                      	if(controllo_data(data1))
                      	
                      	{
                  		
                            data1str = data1.substr(6)+data1.substr(3, 2)+data1.substr(0, 2);
                  			
                  
                  	  if (data1str-data<0) 
                  	  
                  	  {
                              	alert("Error Date");
                            }
                          }
                  
                  else{
                         alert("KO");
                  	   document.orario.ggInizio.focus();
                  	   return false;
                      	}
                          
                  }
                  
                  
                  //-->
                  
                  </script>
                  
                  </HEAD>
                  <BODY>
                  
                  <form name="orario" method="POST" action="" onSubmit="return confronta_data(this);">
                  
                  <input type="text" name="ggInizio" size="10">
                  
                  <input type="submit" value="Invia" name="B1">
                  
                  
                  </form>
                  </BODY>
                  </HTML>

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You forgot the time part. Add the user inputted hours and minutes to the Date object too. When comparing, you can add 2 hours to the now date using setHours().

                    Comment

                    • RamananKalirajan
                      Contributor
                      • Mar 2008
                      • 608

                      #11
                      Hi Viki, u can do it very well in this way.

                      [HTML]var usrDate = new Date(year,month ,day,hour*,min* ,sec*,milli* );[/HTML]

                      This is a parameterized constructor which will return u a date object.
                      Note: The args with * are optional one but it will be useful for u to create a date obj with user given hour and minutes. Any soubts post back i will try to help u out.

                      Regards
                      Ramanan Kalirajan

                      Comment

                      Working...