How to round to the nearest upper 15 minutes in moment.js

Viewed 2250

Using moment.js, I want to round to the nearest 15 minutes but from now on. For example:

10:13am -> 10:15am 11:01am -> 11:15am

2 Answers

This might help you.This uses momentjs

var moment = require("moment");
const start = moment("2018-12-08 09:42");
const remainder = 15 - (start.minute() % 15);
const dateTime = moment(start)
  .add(remainder, "minutes")
  .format("DD.MM.YYYY, h:mm:ss a");
console.log(dateTime);


Sandbox: https://codesandbox.io/s/compassionate-bas-fg1c2

That code below is made with this: https://momentjs.com/. I don't even know java script (sorry for the formatting). If I can do it, you can do it to.

var thismoment = moment();
var now = thismoment.format('MMM DD h:mm A');
var minutes = thismoment.format('mm');
var rest = minutes % 15;
if (rest < 8) {
    alert(thismoment.subtract(rest, 'minutes').calendar());
} else {
    alert(thismoment.add(15 - rest, 'minutes').calendar());
}
<script src="https://momentjs.com/downloads/moment.js"></script>

Related