2

I am working on delivery page which has two radio buttons viz.'Regular day time delivery' and 'Require Midnight Delivery'. I want to display another radio button(viz Same day Delivery) if and only if time is 2 PM and 'Regular day time delivery' is checked Otherwise that button will not be visible or will be disabled.

I can disable button using javascript but don't know how to disable it according to time?

Can anyone Help me?

Update1: I want 'Same day Delivery' button to be visible/enabled after 2PM till 7AM next day.

Update2: I am using PHP-Mysql as Backend/server side Technology

2 Answers2

0

You can do it with setIntertval() function (checking time every minute):

var interv = setInterval(function(){ 
    // from 14 PM till 7 AM
    var hours = (new Date()).getHours();
    if(hours >= 14 || hours < 7) 
        $(".sameDay").hide();
    else
        $(".sameDay").show();
}, 60000);

Here is demo fiddle

Ivan Gerasimenko
  • 2,381
  • 3
  • 30
  • 46
0
<!DOCTYPE html>
<html>
<body onload="myFunction();">

<INPUT TYPE="Radio" Name="payment" onclick="myFunction();" Value="CC">Regular day time delivery
 <INPUT TYPE="Radio" Name="payment" onclick="func2();"  Value="DC">Require Midnight Delivery
 <INPUT TYPE="Radio" Name="payment1" id="1" Value="PP" >Same day Delivery
<input type="text" id="h1">
<input type="text" id="h2">

<script>
function myFunction() {
var d = new Date();
var p=Math.floor((d.getTime()/ (1000*60*60)) % 24)
var q=Math.floor((d.getTime()/ (1000*60)) % 60 )
document.getElementById("h1").value=p;
document.getElementById("h2").value=q;

if((p>=1 && q>=30) && ( p<=8 && q<=30) ) 
{
document.getElementById("1").disabled=false;
}
else
{   
document.getElementById("1").disabled=true;
}
}

function func2()
{
document.getElementById("1").checked = false;
document.getElementById("1").disabled=true;

}
</script>

</body>
</html>

Here i used UTC time so irrespective of client/country you will get standard time.

Also added textbox to check hours.

You can check code here (copy above code) As fiddle is not working http://www.w3schools.com/html/tryit.asp?filename=tryhtml_default

Nagendra Nigade
  • 866
  • 2
  • 12
  • 28
  • 2
    That is according to the client time though – wirey00 Mar 11 '15 at 13:04
  • it should work, it's working fine at my end. as mentioned by E for server side time check this stackoverflow.com/questions/18281346/how-to-get-server-time-using-javascript due to some emergency it's not possible to edit answer, will do updation for server time soon – Nagendra Nigade Mar 11 '15 at 15:11
  • @NagendraNigade Thank you for updated answer. Unable to try it at the moment as its not considering mine local time. Will check after sometime and will update you. Is it possible to convert UTC to IST? And by the way +1 for answer meanwhile. –  Mar 12 '15 at 05:24
  • 1
    @genius `if(p<14 && p>6)`IST eqivalent to `if(p<8 && p>1)`UTC so change condition with `if(p<8 && p>1)` will do for enable 6.30am to 13.30pm IST , ya its possible to do it in IST but if this work around is suitable then go for it – Nagendra Nigade Mar 12 '15 at 06:00
  • @genius : check now... Any specific reason to convert to IST ?? As this will work perectly fine. – Nagendra Nigade Mar 12 '15 at 09:30
  • Thank you @NagendraNigade . Reason is : Website which I am working on is being hosted on server which is located in India. –  Mar 13 '15 at 05:27