如何舍入秒数:
介于1 sec - 59 sec
至1 minute
在1 min 1 sec
-1 min 59 sec
之间->舍入到2 m
在1h 2 min 1 sec
-2 min 59 sec
之间->舍入到1h 3 m
吗?
在2 min 1 sec
-2 min 59 sec
之间->舍入到3 m
吗?
我使用库“ moment”
预期效果:
Date2-date1 =结果四舍五入为整分钟。
date2-date1 = 30 s->舍入为1 m
date2-date1 = 5s->舍入为1 m
date2-date1 = 1m 10s->舍入为2 m
javascript或moment.js
库中可能有一种方法
a = (d1, d2) => {
let b= moment(d2).diff(d1,"minutes",true)
return Math.round(b) * 60;
}
不同的1m 20 s->舍入到2m->没关系
5s不同->不要舍入到1 m // problem here
答案 0 :(得分:1)
您可以在秒数上增加59秒,然后增加分钟数。
console.log([0, 1, 59, 60, 61].map(v => Math.floor((v + 59) / 60)));
答案 1 :(得分:1)
尝试一下,虽然有点基本,但可以使用:
const MAX_MIN_SEC = 60;
const MAX_HRS = 24;
const MAX_DAYS = 365;
var date1 = new Date(2018, 07, 00, 00, 00, 00);
var date2 = new Date(2018, 07, 00, 00, 00, 05);
var msec = date2 - date1;
var seconds = Math.floor(msec / 1000);
var mins = Math.floor(seconds / MAX_MIN_SEC);
var hrs = Math.floor(mins / MAX_MIN_SEC);
var days = Math.floor(hrs / MAX_HRS);
var yrs = Math.floor(days / MAX_DAYS);
seconds = seconds % MAX_MIN_SEC;
mins = mins % MAX_MIN_SEC;
hrs = hrs % MAX_HRS;
days = days % MAX_DAYS;
if (seconds > 0){
seconds = 0;
mins++;
}
if (mins >= MAX_MIN_SEC){
mins = 0;
hrs++;
}
if (hrs >= MAX_HRS){
hrs = 0;
days++;
}
if (days >= MAX_DAYS){
days = 0;
yrs++;
}
Console.log(yrs + " year/s, " + days + " day/s, " + hrs + " hour/s, " + mins + " minute/s," + seconds + " second/s");
结果:
0 years, 0 days, 0 hours, 1 minutes,0 seconds
答案 2 :(得分:1)
您正在搜索Math.ceil()
const a = (d1, d2) => {
let b = moment(d2).diff(d1, "minutes", true)
return Math.ceil(b)
}
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:43:18'))
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:43:59'))
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:44:59'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js" integrity="sha256-H9jAz//QLkDOy/nzE9G4aYijQtkLt9FvGmdUTwBk6gs=" crossorigin="anonymous"></script>
更新
Moment直到现在才支持duration().format()
。但是您可以尝试这种解决方法(如果需要,您需要处理数天)
const a = (d1, d2) => {
let b = moment(d2).diff(d1, "minutes", true)
let d = moment.duration({
minutes: Math.ceil(b)
})
return moment(d._data).format('H[h] m[m]').replace(/^0h\s|\s0m/, '')
}
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:43:18'))
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:43:59'))
console.log(a('2019-07-05 10:43:18', '2019-07-05 20:44:59'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js" integrity="sha256-H9jAz//QLkDOy/nzE9G4aYijQtkLt9FvGmdUTwBk6gs=" crossorigin="anonymous"></script>
const a = (d1, d2) => {
let b = moment(d2).diff(d1, "minutes", true)
return moment.duration({
minutes: Math.ceil(b)
}).format("d[d] h[h] m[m]", { trim: "both" });
}
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:43:18'))
console.log(a('2019-07-05 10:43:18', '2019-07-05 10:43:59'))
console.log(a('2019-07-05 10:43:18', '2019-07-07 20:44:59'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js" integrity="sha256-H9jAz//QLkDOy/nzE9G4aYijQtkLt9FvGmdUTwBk6gs=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/2.3.2/moment-duration-format.min.js" integrity="sha256-M2KULKSJyw+G0068JiMlM9GX4XpLdUButSzBqntKDZM=" crossorigin="anonymous"></script>
答案 3 :(得分:0)
Sjoerd Loeve对this possible duplicate的回答包括一个指向这样做的干净方法的链接https://jsfiddle.net/2wqs4o0v/3:
var now = new moment(new Date());
if (now.seconds() > 0) {
now.add('minutes', 1);
}
JSFiddle不会导入矩(SO也不会),但这看起来可以工作,并且如果您想将时间取整为0秒但大于0毫秒,则可以添加&& now.milliseconds() > 0
。 / p>