如何在javascript中将1.5小时更改为1小时30分钟



我想从两个日期时间中获取剩余时间,并将其显示为时间格式


function roundDown(floating) {
var rounded = Math.round(floating * 100) / 100;
return rounded;
}
const start = new Date("2020-12-03T11:30:00Z").getTime() / (1000 * 3600);
const end = new Date("2020-12-03T13:00:00Z").getTime() / (1000 * 3600)
let total = roundDown(end - start);
// it returns 1.5 
// I want it converted to 1hr 30min

您可以将小数点后的值乘以60

function roundDown(floating) {
var rounded = Math.round(floating * 100) / 100;
return rounded;
}
const start = new Date("2020-12-03T11:30:00Z").getTime() / (1000 * 3600);
const end = new Date("2020-12-03T13:00:00Z").getTime() / (1000 * 3600)

let total = roundDown(end - start);

//just add these lines of code
const remaining = total - Math.floor(total);
const minutes = 60 * remaining;
console.log("Minutes : " + Math.round(minutes) + "tHours : " + Math.floor(total));

您想要的是简单的字符串操作:

const totalTime = total.getHours().toString() + " : " + total.getMinutes().toString()

如果要将1.5小时转换为1小时30分钟,只需将小数部分乘以60(例如0.5*60=30(。我真的不确定我是否理解你的问题。

function roundDown(floating) {
var rounded = Math.round(floating * 100) / 100;
return rounded;
}
const start = new Date("2020-12-03T11:30:00Z").getTime() / (1000 * 3600);
const end = new Date("2020-12-03T13:00:00Z").getTime() / (1000 * 3600)
let total = roundDown(end - start);
console.log(total)
var hour = Math.floor(total);
var decimal = total - hour;
var min = 1 / 60;
// Round to nearest minute
decimal = min * Math.round(decimal / min);
var minute = Math.floor(decimal * 60);
console.log(minute)

最新更新