-1

I am a newbie in java script and I need help on how to get the time difference between now and maybe 9PM today (Assuming the time now is not up to 9PM). If possible, I want it especially, the seconds part to be changing/reducing dynamically. H: M S format please. I'll appreciate your help pls

Yormie
  • 31
  • 6
  • Any effort in trying to solve this yuourself? – putvande Jul 30 '14 at 15:56
  • possible duplicate of [How do I get the difference between two Dates in JavaScript?](http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript) – jprofitt Jul 30 '14 at 15:57

2 Answers2

2

Use this: * Updated - With Days Hours Minutes and Seconds *

showDiff();

function showDiff(){
    var date1 = new Date();    
    var date2 = new Date("2015/07/30 21:59:00");
    //Customise date2 for your required future time

    var diff = (date2 - date1)/1000;
    var diff = Math.abs(Math.floor(diff));

    var days = Math.floor(diff/(24*60*60));
    var leftSec = diff - days * 24*60*60;

    var hrs = Math.floor(leftSec/(60*60));
    var leftSec = leftSec - hrs * 60*60;

    var min = Math.floor(leftSec/(60));
    var leftSec = leftSec - min * 60;

    document.getElementById("showTime").innerHTML = "You have " + days + " days " + hrs + " hours " + min + " minutes and " + leftSec + " seconds before death.";

setTimeout(showDiff,1000);
}

for your HTML COde:

<div id="showTime"></div>
tika
  • 7,135
  • 3
  • 51
  • 82
  • Thanks for your response. Is it possible for me set date2 variable as 9PM today without having to hand code 2015/07/30? – Yormie Jul 30 '14 at 16:55
0

This is some untested code which should will give you hours and minutes until 9:00PM

var hoursToday = 21; // the 9 in 9:00 PM
var minutesToday = 0; // the 00 in 9:00 PM

var d = new Date();
var hoursNow = d.getHours();
var minutesNow = d.getMinutes();

var hoursLeft = hoursToday - hoursNow;
var minutesLeft = minutesToday - minutesNow;
Friedrich
  • 2,211
  • 20
  • 42