We help IT Professionals succeed at work.

Could you point how to correctly validate if the time before the date/ time filled on the textbox is greater or equal 03 hours?

Eduardo Fuerte
on
75 Views
Last Modified: 2019-02-05
Hi Experts

Could you point how to correctly validate if the time before the date/ time filled on the textbox is greater or equal 03 hours?

Accordingly to:
img002
<td id="div_data_missao_ida_1" style="visibility: <?=(isset($guia['missao'][0]['nMissaoPreMarcada']) && $guia['missao'][0]['nMissaoPreMarcada']==1 ?"visible":"visible")?>;">
	<label class="form_title">Data da Miss&atilde;o:</label><br/>
	<input type="text"  class="input" name="dpremarcada_ida" id="dpremarcada_ida" value="<?=(isset($guia['missao'][0]['dMissao'])?formataStamp($guia['missao'][0]['dMissao'],"stamp","dma"):date("d/m/Y"))?>" size="11" maxlength="10"  onchange="copiaDataMissaoTIT();">
	<img src="imagens/calendario.png" name="cal_trigger" id="cal_trigger" border="0" align="absmiddle" onmouseover="javascript: calendar_param('dpremarcada_ida', 'cal_trigger', isAnterior);" style="cursor: pointer;" >
</td>


<td id="div_data_missao_ida_2" style="visibility: <?=(isset($guia['missao'][0]['nMissaoPreMarcada']) && $guia['missao'][0]['nMissaoPreMarcada']==1 ?"visible":"visible")?>;">
	<label class="form_title">Hor&aacute;rio:</label><br/>
	<input type="text"  onkeypress="campo_hora(event); return validaDigNum(event);" value="<?=(isset($guia['missao'][0]['dMissao'])?formataStamp($guia['missao'][0]['dMissao'],"stamp","hm"):date("H:i"))?>" size="5" maxlength="5" name="horario_premarcada_ida" id="horario_premarcada_ida" class="input" >            
</td>

Open in new window


The dtae/ time filled in the textbox must to be greater than 03 hours.

Could you suggest how to solve?

Thanks in advance
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi @Julian

The code runs at front-end.
I'm looking your solution, but the working sample is missed.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Sorry Eduardo,

The link text was overwritten in my original post  I have fixed the post and posted the URL here again

http://www.marcorpsa.com/ee/t3504.html
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi @Julian

The server where the example runs seens not working out today.
Could you give a look?
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Ok. Now.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Could you check what is going wrong on my case?
img001
Adapted to my case
function pre_salvar_captacao_tit(validaAnteriores) {
  try {
    
    
      var now = Date.now();
      var d1 = document.getElementById('dpremarcada_ida_stacasa').value;
      var t1 = document.getElementById('horario_premarcada_ida_stacasa').value;   


      var formtime = new Date(d1 + ' ' + t1);

      console.log('XXXX');
      console.log(now);
      
      console.log(d1);
      console.log(t1);
      
      console.log(formtime);
      
      
      
     if (  ( (formtime.getTime() - now) / 3600000)   < 3   )   {
            alert('Agendar com no mínimo 03 horas de antecedencia');
     }      
     else
     {
         alert('Agendar com no mínimo 03 horas de antecedencia');
    }

Open in new window



 alert('Agendar com no mínimo 03 horas de antecedencia');  is always fired  even if the hour is later than 03 hours.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
I just tested your code in my sample and it seems to work fine.

You can see it working here
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
.... but then I did notice that your Alerts have the same message
if (  ( (formtime.getTime() - now) / 3600000)   < 3   )   {
  alert('Agendar com no mínimo 03 horas de antecedencia'); // HOW IS THIS 
}      
else {
  alert('Agendar com no mínimo 03 horas de antecedencia'); // DIFFERENT FROM THIS ??????
}

Open in new window

Eduardo FuerteDeveloper and Analyst

Author

Commented:
Sorry

     if (  ( (formtime.getTime() - now) / 3600000)   < 3  )   {
            alert('Agendar com no mínimo 03 horas de antecedencia');
     }      
     else
     {
         alert('Agendamento OK');
    }

Open in new window


Even if the time difference is lower than 3 hours
alert('Agendamento OK');
is fired.

I don't know why the console stoped running.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
The value is never <3

img002

     console.log('h1');
      var h1 = (formtime.getTime()) / 3600000
      console.log(h1);
            
      console.log('h2');
      var h2 = ( now) / 3600000
      console.log(h2);             
       

      var teste = (formtime.getTime() - now) / 3600000
      console.log('Teste');
      console.log(teste);
      
      
     if (  ( (formtime.getTime() - now) / 3600000)   < 3  )   {
            alert('Agendar com no mínimo 03 horas de antecedencia');
     }      
     else
     {
         alert('Agendamento OK');
    }

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
If you look at my latest sample - it implements your function AND mine - yours is called after. As you can see from that sample it works.

So this is probably something on your side that you are not showing here?
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Really.

I don´t know why the date/ time is converted 3 hours later...

JS:
function pre_salvar_captacao_tit(validaAnteriores) {
  try {
    
        var now = Date.now();
        
        var d1 = document.getElementById('dpremarcada_ida_stacasa').value;
        var t1 = document.getElementById('horario_premarcada_ida_stacasa').value;   


        console.log('d1');
        console.log(d1);

        console.log('t1');
        console.log(t1);


      //var formtime = new Date(this.date.value + ' ' + this.time.value);
       var formtime = new Date(d1 + ' ' + t1);

       console.log('formtime');
       console.log(formtime);

Open in new window


img003
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Because getTime() returns UTC time - your timezone is what is creating the difference.

Where are you based? This is suggesting you are somewhere west of Greenwhich (GMT-3) - is that correct?

The algorithm should still work though - because both times are in UTC - so the hour difference should still work out.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Well...
In the meanwhile I did this workaround
function pre_salvar_captacao_tit(validaAnteriores) {
  try {
    
        var now = Date.now();
        
        var d1 = document.getElementById('dpremarcada_ida_stacasa').value;
        var t1 = document.getElementById('horario_premarcada_ida_stacasa').value;   

       // This formats correctly what is presented at the form:
        var data = new Date(),
        dia  = data.getDate().toString(),
        diaF = (dia.length == 1) ? '0'+dia : dia,
        mes  = (data.getMonth()+1).toString(), //+1 pois no getMonth Janeiro começa com zero.
        mesF = (mes.length == 1) ? '0'+mes : mes,
        anoF = data.getFullYear();
       
        formtime = new Date(anoF+"/"+mesF+"/"+diaF+ ' ' + t1);

      
     if (  ( (formtime.getTime() - now) / 3600000)   < 3  )   {
            alert('Agendar com no mínimo 03 horas de antecedencia');
     }      
     else
     {
         alert('Agendamento OK');
    }

Open in new window


So, it finally runs:

Difference of time greather than 03 hours => OK
img004
Difference of time less than 03 hours => NOK
img005
Well, it's running correctly.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
That shouldn't be necessary but if it is working ....

Just checked your profile and I see you are in Brazil which is UTC-3 - which means the values you are seeing for the getTime() are correct.

What do you get when you dump the now value?

console.log(now);
Eduardo FuerteDeveloper and Analyst

Author

Commented:
This:
1549309222818
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Ok I need to see both Now and the converted timestamp otherwise I can't do a comparison.

Can you do this.
console.log(now);
console.log(fromtime.getTime());

Open in new window

Eduardo FuerteDeveloper and Analyst

Author

Commented:
Here it is:
img001
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Those are only 187 seconds apart?
Eduardo FuerteDeveloper and Analyst

Author

Commented:
I didn't change the textbox value.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Then the < 3 message should show.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Outstanding assistance!
Thank you.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are welcome.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions