Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag for Brazil asked on

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

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
PHPJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Eduardo Fuerte

ASKER
Hi @Julian

The code runs at front-end.
I'm looking your solution, but the working sample is missed.
Julian Hansen

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 Fuerte

ASKER
Hi @Julian

The server where the example runs seens not working out today.
Could you give a look?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Eduardo Fuerte

ASKER
Ok. Now.
Eduardo Fuerte

ASKER
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.
Julian Hansen

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

You can see it working here
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Julian Hansen

.... 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 Fuerte

ASKER
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 Fuerte

ASKER
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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Julian Hansen

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 Fuerte

ASKER
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
Julian Hansen

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Eduardo Fuerte

ASKER
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.
Julian Hansen

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 Fuerte

ASKER
This:
1549309222818
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Julian Hansen

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 Fuerte

ASKER
Here it is:
img001
Julian Hansen

Those are only 187 seconds apart?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Eduardo Fuerte

ASKER
I didn't change the textbox value.
Julian Hansen

Then the < 3 message should show.
Eduardo Fuerte

ASKER
Outstanding assistance!
Thank you.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Julian Hansen

You are welcome.