Javascript 如何使用 jQuery 将分钟转换为小时/分钟并将各种时间值添加在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4687723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert minutes to hours/minutes and add various time values together using jQuery?
提问by sevens
There are a couple parts to this question. I am not opposed to using a jQuery plugin if anyone knows of one that will accomplish what I want done.
这个问题有几个部分。如果有人知道可以完成我想做的事情,我不反对使用 jQuery 插件。
Q1 - How do I convert minutes into hours and vise versa? For example how would I convert 90 minutes into 1 hour and 30 minutes. In the code below how would I get "totalMin" converted and displayed in "convertedHour" and "convertedMin"?
Q1 - 如何将分钟转换为小时,反之亦然?例如,我如何将 90 分钟转换为 1 小时 30 分钟。在下面的代码中,如何将“totalMin”转换并显示在“convertedHour”和“convertedMin”中?
HTML:
HTML:
<span class="totalMin">90</span> Minutes
<span class="convertedHour">0</span> Hours
<span class="convertedMin">0</span> Minutes
jsFiddle: http://jsfiddle.net/ExFBD
jsFiddle:http: //jsfiddle.net/ExFBD
Q2 - How would I add up a group of time slots? For example, How would I add 1 hour 30 mins, 2 hours 45 mins and 2 hour 15 mins together?
Q2 - 我如何将一组时间段加起来?例如,我如何将 1 小时 30 分钟、2 小时 45 分钟和 2 小时 15 分钟加在一起?
HTML:
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/
jsFiddle:http: //jsfiddle.net/DX9YA/
Q3 - How would I take a time value (3 hours and 30 mins) and add it to a real time stamp such as 10:30 AM? How would I take AM vs PM into account?
Q3 - 我如何获取时间值(3 小时 30 分钟)并将其添加到实时时间戳,例如上午 10:30?我将如何考虑 AM 与 PM?
回答by nunopolonia
Q1:
问题 1:
$(document).ready(function() {
var totalMinutes = $('.totalMin').html();
var hours = Math.floor(totalMinutes / 60);
var minutes = totalMinutes % 60;
$('.convertedHour').html(hours);
$('.convertedMin').html(minutes);
});
Q2:
问题 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);
});
回答by ConorLuddy
In case you want to return a string in 00:00 format...
如果您想以 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;
}
Thanks for the up-votes. Here's a slightly tidier ES6 version :)
感谢您的投票。这是一个稍微整洁的 ES6 版本:)
function convertMinsToHrsMins(mins) {
let h = Math.floor(mins / 60);
let m = mins % 60;
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
return `${h}:${m}`;
}
回答by Dan S.
The function below will take as input # of minutes and output time in the following format: Hours:minutes. I used Math.trunc(), which is a new method added in 2015. It returns the integral part of a number by removing any fractional digits.
下面的函数将采用以下格式的输入分钟数和输出时间:小时:分钟。我使用了 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"
回答by Илья Зеленько
Very short code for transferring minutes in two formats!
以两种格式传输分钟的非常短的代码!
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"
回答by Malk
function parseMinutes(x) {
hours = Math.floor(x / 60);
minutes = x % 60;
}
function parseHours(H, M) {
x = M + H * 60;
}
回答by Maxwelll
var timeConvert = function(n){
var minutes = n%60
var hours = (n - minutes) / 60
console.log(hours + ":" + minutes)
}
timeConvert(65)
this will log 1:5 to the console. It is a short and simple solution that should be easy to understand and no jquery plugin is necessary...
这将 1:5 记录到控制台。这是一个简短而简单的解决方案,应该易于理解,并且不需要 jquery 插件......
回答by Spittal
I took the liberty of modifying ConorLuddy's answer to address both 24 hour time and 12 hour time.
我冒昧地修改了 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}`;
}
}
回答by kostest kaptest
This code can be used with timezone
此代码可与时区一起使用
javascript:
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"
回答by Jarkko Oksanen
Alternated to support older browsers.
交替支持旧浏览器。
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;
}
}
回答by Jon McAuliffe
Not a jquery plugin, but the DateJS Libraryappears to do what you require. The Getting Started pagehas a number of examples.