By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Help with check date and hours

100+
P: 263
Help with check date and hours

Hi all.

I have this form:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4.  
  5. </head>
  6.  
  7. <body>
  8.  
  9. <form name="orario" method="post" action="">
  10.  
  11. <input type="text" name="hour" size="20">
  12.  
  13. <input type="text" name="dates" size="20">
  14.  
  15. <input type="submit" value="Invia" name="B1">
  16.  
  17. </form>
  18. </body>
  19.  
  20. </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
Sep 22 '08 #1
Share this Question
Share on Google+
10 Replies


RamananKalirajan
100+
P: 607
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
Sep 22 '08 #2

100+
P: 263
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?
Sep 22 '08 #3

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 22 '08 #4

100+
P: 263
Hi acoder; one example please?
Sep 22 '08 #5

acoder
Expert Mod 15k+
P: 16,027
This link should help get you started.
Sep 22 '08 #6

100+
P: 263
This link should help get you started.
No, I dont no... I dont understand your suggestion...
Sep 22 '08 #7

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 22 '08 #8

100+
P: 263
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... :

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. <!--
  3.  
  4. function controllo_data(stringa){
  5.         var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  6.           if (!espressione.test(stringa))
  7.           {
  8.             return false;
  9.           }
  10.       else{
  11.              anno = parseInt(stringa.substr(6),10);
  12.              mese = parseInt(stringa.substr(3, 2),10);
  13.              giorno = parseInt(stringa.substr(0, 2),10);
  14.  
  15.          var data=new Date(anno, mese-1, giorno);
  16.                if(data.getFullYear()==anno && data.getMonth()+1==mese && data.getDate()==giorno){
  17.                     return true;
  18.                 }
  19.            else{
  20.                  return false;
  21.                }
  22.            }
  23. }
  24.  
  25. function confronta_data(data1)
  26.  
  27. {
  28.     dataCorr = new Date();
  29.     giorno = dataCorr.getDate();
  30.     mese = dataCorr.getMonth();
  31.     anno = dataCorr.getYear();
  32.     data = anno+mese+giorno;
  33.  
  34.         if(controllo_data(data1))
  35.  
  36.         {
  37.  
  38.           data1str = data1.substr(6)+data1.substr(3, 2)+data1.substr(0, 2);
  39.  
  40.  
  41.       if (data1str-data<0) 
  42.  
  43.       {
  44.                 alert("Error Date");
  45.           }
  46.         }
  47.  
  48. else{
  49.        alert("KO");
  50.        document.orario.ggInizio.focus();
  51.        return false;
  52.         }
  53.  
  54. }
  55.  
  56.  
  57. //-->
  58.  
  59. </script>
  60.  
  61. </HEAD>
  62. <BODY>
  63.  
  64. <form name="orario" method="POST" action="" onSubmit="return confronta_data(this);">
  65.  
  66. <input type="text" name="ggInizio" size="10">
  67.  
  68. <input type="submit" value="Invia" name="B1">
  69.  
  70.  
  71. </form>
  72. </BODY>
  73. </HTML>
Sep 22 '08 #9

acoder
Expert Mod 15k+
P: 16,027
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().
Sep 23 '08 #10

RamananKalirajan
100+
P: 607
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
Sep 23 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.