我是java脚本的新手,我需要帮助来解决现在和今天晚上9点之间的时差(假设现在的时间不到晚上9点)。如果可能的话,我特别想要它,秒部分动态地改变/减少。 H:M S格式请。我很感激你的帮助
答案 0 :(得分:2)
使用此: * 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);
}
为您的HTML COde:
<div id="showTime"></div>
答案 1 :(得分:0)
这是一些未经测试的代码,应该会在下午9:00之前为您提供小时和分钟
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;