0

How to round seconds:

between 1 sec - 59 sec to 1 minute

between 1 min 1 sec - 1 min 59 sec --> round to 2 m

between 1h 2 min 1 sec - 2 min 59 sec --> round to 1h 3 m?

between 2 min 1 sec - 2 min 59 sec --> round to 3 m?

I use library 'moment'


Expected effect:

Date2 - date1 = The result is rounded to full minutes.

date2 - date1 = 30 s -> round to 1 m

date2 - date1 = 5s -> round to 1 m

date2 - date1 = 1m 10s -> round to 2 m

There may be a way in javascript or in the moment.js library

a = (d1, d2) => {
  let b= moment(d2).diff(d1,"minutes",true)

  return  Math.round(b) * 60; 
}

different 1m 20 s --> round to 2m --> it's ok

different 5s --> don't round to 1 m // problem here

Umbro
  • 1,984
  • 12
  • 40
  • 99
  • 3
    Why? `60` is not a valid value for seconds - at least not in case of a time entry. – Andreas Jun 26 '19 at 09:44
  • 1
    No clue what your “examples” are supposed to mean either. – 04FS Jun 26 '19 at 09:44
  • Doesn't `0:0:1` mean 0 days, 0 hours, 1 minute? – GrafiCode Jun 26 '19 at 09:46
  • https://momentjs.com/docs/#/durations/seconds/ _“will return a number between 0 and 59”_ - so you are not ever going to get a value > 60 with that last part anyway, and if you want to round up all numbers < 60 _to_ 60, you might as well append the _static text literal_ `60` at the end. – 04FS Jun 26 '19 at 09:49
  • Sorry. I updated my answer. I want to get the result 0: 1: 0 – Umbro Jun 26 '19 at 09:52
  • @Mark date2 - date1 = 5s --> don't round to 1 min --> it doesn't work(problem here); But if I have date2-date1 = 1m 20 s --> around to 2 min --> it works – Umbro Jul 04 '19 at 00:38
  • @Andreas yes it is: See [Leap Seconds](https://en.wikipedia.org/wiki/Leap_second) The most recent of which was on December 31, 2016 at 23:59:60 UTC – Wyck Jul 04 '19 at 01:59

4 Answers4

2

You are searching for 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>

UPDATE

Moment doesn't support duration().format() untill now. But you can try this workaround (you need to handle days if required)

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>

OR using moment-duration-format

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>
User863
  • 19,346
  • 2
  • 17
  • 41
1

You could add 59 seconds to the seconds and get floored minutes.

console.log([0, 1, 59, 60, 61].map(v => Math.floor((v + 59) / 60)));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Sjoerd Loeve's answer to this possible duplicate includes a link to a clean way of doing this https://jsfiddle.net/2wqs4o0v/3:

var now = new moment(new Date());

if (now.seconds() > 0) {
    now.add('minutes', 1);
}

JSFiddle doesn't import moment (and neither does SO), but that looks like it'll work, and you can add && now.milliseconds() > 0 if you want to round up times with 0 seconds but > 0 milliseconds.

Aaron
  • 2,049
  • 4
  • 28
  • 35
1

Try this, it's a bit basic but it will work:

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");

Result:

0 years, 0 days, 0 hours, 1 minutes,0 seconds
Mark Melgo
  • 1,477
  • 1
  • 13
  • 30