我遇到了一些问题,希望将剩余时间显示为小时和分钟。
现在的问题是,在计算时间时,它显示例如1.97
小时。我希望时间像1.30
(1小时30分钟)。
我还面临到达时间的问题,我希望将剩余时间计算到下一个open
。
下面是我到目前为止完成的代码:
var openTime = [{
open: -1,
close: -1
}, {
open: 8,
close: 21
}, {
open: 8,
close: 21
}, {
open: 8,
close: 21
}, {
open: 8,
close: 21
}, {
open: 8,
close: 21
}, {
open: 8,
close: 17
}]
var current = new Date();
var day = current.getDay();
var nextday = current.getDay()+1;
var currentTime = current.getHours() + (current.getMinutes() / 60);
var remainTime = 0;
if (openTime[day].close - (currentTime).toFixed(2) >= 0) {
remainTime = (openTime[day].close - currentTime).toFixed(2)
console.log("close in %s hours", remainTime);
document.getElementById("hours").innerHTML = "close in " + remainTime+ " hours"
}
else if (openTime[day].close - (currentTime).toFixed(2) <= 0) {
remainTime = (currentTime).toFixed(2) + (openTime[nextday].open)
document.getElementById("hours").innerHTML = "open in " + remainTime+ " hours"
}
<div id="hours"></div>
答案 0 :(得分:3)
function timeConvert(n) {
var num = n;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
return num + " minutes = " + rhours + " hour(s) and " + rminutes + " minute(s).";
}
console.log(timeConvert(200));
我想你可以这样做。
答案 1 :(得分:0)
尝试这样。将您的打开和关闭时间转换为分钟,而当前时间也转换为分钟,并计算剩余时间
var openTime = [{
open: -1,
close: -1
}, {
open: 8,
close: 21
}, {
open: 8,
close: 21
}, {
open: 8,
close: 10
}, {
open: 8,
close: 21
}, {
open: 8,
close: 21
}, {
open: 8,
close: 17
}]
var current = new Date();
var day = current.getDay();
var nextday = current.getDay()+1;
var currentTime = currentTime = (current.getHours() * 60) + current.getMinutes();
var remainTime = 0;
var closeTime = (openTime[day].close *60);
var openTime = (openTime[day].open *60);
if (closeTime - currentTime >= 0) {
remainTime = closeTime - currentTime;
console.log("close in %s minutes", remainTime);
var hours = Math.floor(remainTime/60);
if(hours == 0){
document.getElementById("hours").innerHTML = "close in " + Math.floor(remainTime%60) + " Mins";
} else {
document.getElementById("hours").innerHTML = "close in " + hours+ " hours " + Math.floor(remainTime%60) + " Mins";
}
}
else if (closeTime - currentTime <= 0) {
remainTime = currentTime + openTime;
document.getElementById("hours").innerHTML = "open in " + Math.floor(remainTime/60)+ " hours" + Math.floor(remainTime%60) + " Mins";
}
<div id="hours"></div>
答案 2 :(得分:0)
请调整关闭功能参数中的关闭时间以进行测试。希望对您有所帮助。
function closing(closing_time) {
let current_datetime = new Date();
let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate()
var closing = new Date(formatted_date + " "+closing_time);
// get total seconds between the times
var delta = Math.abs(current_datetime - closing) / 1000;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
//alert(minutes);
alert("Remaining time to go home : " + hours + "hours " + minutes + "minutes");
}
closing("06:30 PM");