这个问题有几个部分。如果有人知道可以完成我想要完成的任务的话,我并不反对使用 jQuery 插件。
Q1 - 如何将分钟转换为小时,反之亦然?例如我如何将 90 分钟转换为 1 小时 30 分钟。在下面的代码中,如何将“totalMin”转换并显示在“convertedHour”和“convertedMin”中?
HTML:
<span class="totalMin">90</span> Minutes
<span class="convertedHour">0</span> Hours
<span class="convertedMin">0</span> Minutes
jsFiddle:http://jsfiddle.net/ExFBD
Q2 - 如何添加一组时间段?例如,如何将 1 小时 30 分钟、2 小时 45 分钟和 2 小时 15 分钟加在一起?
HTML:
<span class="hour-1 hour">1</span> hour
<span class="min-1 min">30</span> min
<span class="hour-2 hour">2</span> hour
<span class="min-2 min">45</span> min
<span class="hour-3 hour">2</span> hour
<span class="min-3 min">15</span> min
<span class="totalHour">0</span> hour
<span class="totalMin">0</span> min
jsFiddle:http://jsfiddle.net/DX9YA/
Q3 - 我如何获取时间值(3 小时 30 分钟)并将其添加到实时时间戳(例如上午 10:30)?我如何考虑 AM 与 PM?
问题1:
$(document).ready(function() {
var totalMinutes = $('.totalMin').html();
var hours = Math.floor(totalMinutes / 60);
var minutes = totalMinutes % 60;
$('.convertedHour').html(hours);
$('.convertedMin').html(minutes);
});
问题2:
$(document).ready(function() {
var minutes = 0;
$('.min').each(function() {
minutes = parseInt($(this).html()) + minutes;
});
var realmin = minutes % 60
var hours = Math.floor(minutes / 60)
$('.hour').each(function() {
hours = parseInt($(this).html()) + hours;
});
$('.totalHour').html(hours);
$('.totalMin').html(realmin);
});
如果您想返回 00:00 格式的字符串...
convertMinsToHrsMins: function (minutes) {
var h = Math.floor(minutes / 60);
var m = minutes % 60;
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
return h + ':' + m;
}
感谢您的投票。这是一个稍微整洁的 ES6 版本:)
const convertMinsToHrsMins = (mins) => {
let h = Math.floor(mins / 60);
let m = mins % 60;
h = h < 10 ? '0' + h : h; // (or alternatively) h = String(h).padStart(2, '0')
m = m < 10 ? '0' + m : m; // (or alternatively) m = String(m).padStart(2, '0')
return `${h}:${m}`;
}
下面的函数将输入分钟数和输出时间,格式如下:小时:分钟。 我使用了 Math.trunc(),这是 2015 年添加的新方法。它通过删除任何小数位来返回数字的整数部分。
function display(a){
var hours = Math.trunc(a/60);
var minutes = a % 60;
console.log(hours +":"+ minutes);
}
display(120); //"2:0"
display(60); //"1:0:
display(100); //"1:40"
display(126); //"2:6"
display(45); //"0:45"
👍 非常短的代码,用于以两种格式传输分钟!
let format1 = (n) => `${n / 60 ^ 0}:` + n % 60
console.log(format1(135)) // "2:15"
console.log(format1(55)) // "0:55"
let format2 = (n) => `0${n / 60 ^ 0}`.slice(-2) + ':' + ('0' + n % 60).slice(-2)
console.log(format2(135)) // "02:15"
console.log(format2(5)) // "00:05"
var timeConvert = function(n){
var minutes = n%60
var hours = (n - minutes) / 60
console.log(hours + ":" + minutes)
}
timeConvert(65)
这会将 1:5 记录到控制台。这是一个简短的解决方案,应该很容易理解,并且不需要 jquery 插件......
function parseMinutes(x) {
hours = Math.floor(x / 60);
minutes = x % 60;
}
function parseHours(H, M) {
x = M + H * 60;
}
我擅自修改了 ConorLuddy 的答案以解决 24 小时时间和 12 小时时间问题。
function minutesToHHMM (mins, twentyFour = false) {
let h = Math.floor(mins / 60);
let m = mins % 60;
m = m < 10 ? '0' + m : m;
if (twentyFour) {
h = h < 10 ? '0' + h : h;
return `${h}:${m}`;
} else {
let a = 'am';
if (h >= 12) a = 'pm';
if (h > 12) h = h - 12;
return `${h}:${m} ${a}`;
}
}
此代码可与时区一起使用
javascript:
let minToHm = (m) => {
let h = Math.floor(m / 60);
h += (h < 0) ? 1 : 0;
let m2 = Math.abs(m % 60);
m2 = (m2 < 10) ? '0' + m2 : m2;
return (h < 0 ? '' : '+') + h + ':' + m2;
}
console.log(minToHm(210)) // "+3:30"
console.log(minToHm(-210)) // "-3:30"
console.log(minToHm(0)) // "+0:00"
minToHm(210)
"+3:30"
minToHm(-210)
"-3:30"
minToHm(0)
"+0:00"
由于ConnorLuddy的上述答案可以稍微改进,因此对公式进行了微小的更改,将分钟转换为
hours:mins
格式
const convertMinsToHrsMins = (mins) => {
let h = Math.floor(mins / 60);
let m = Math.round(mins % 60);
h = (h < 10) ? ('0' + h) : (h);
m = (m < 10) ? ('0' + m) : (m);
return `${h}:${m}`;
}
不是 jquery 插件,但 DateJS Library 似乎可以满足您的要求。 入门页面有许多示例。
替代支持旧版浏览器。
function minutesToHHMM (mins, twentyFour) {
var h = Math.floor(mins / 60);
var m = mins % 60;
m = m < 10 ? '0' + m : m;
if (twentyFour === 'EU') {
h = h < 10 ? '0' + h : h;
return h+':'+m;
} else {
var a = 'am';
if (h >= 12) a = 'pm';
if (h > 12) h = h - 12;
return h+':'+m+a;
}
}
const minutesToHour = totalMinutes => {
if (!totalMinutes) return `${0}m`;
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
const h = hours > 0 ? `${hours?.toFixed()}h` : '';
const m = minutes > 0 ? ` ${minutes?.toFixed()}m` : '';
return `${h} ${m}`;
};
console.log(minutesToHour(90))