Umbro
Umbro

Reputation: 2204

Difference between dates, rounded result to nearest minute

The difference between dates wants to round to the nearest minute. Round date1,date2 down or up. The returned result is already rounded up to the full minute. I can modify date1,date2. Do not modify the result already returned

date2- date1

Code here: https://stackblitz.com/edit/react-azau4g

Example:

First step

this.state = {
  date1: "2019-06-29 21:25:38+00",
  date2: "2019-06-29T21:25:40.000+00:00"
}

round = (item) => {
  var m = moment(item);

  var roundUp = (m.second() || m.millisecond() ? m.add(1, 'minute').startOf('minute') : m.startOf('minute')).toISOString();
  return roundUp;
}

differentTime = {
    date1: this.state.date1.toISOString(),  
    date2: this.round(this.state.date2)   //return "2019-06-29T21:26:00.000+00:00"
}

Second step

Expected effect:

data2 - data1 = 1 min


Example 2

 this.state = {
      date1: "2019-06-29 21:25:01+00",
      date2: "2019-06-29T21:27:20.000+00:00"
    }



 differentTime2 = {
        date1: this.state.date1.toISOString(), 
        date2: this.round(this.state.date2)   //return "2019-06-29T21:28:00.000+00:00"
    }

Expecting effect:

date2 - date1 = 3 min

Upvotes: 1

Views: 435

Answers (3)

Hassaan Saleem
Hassaan Saleem

Reputation: 36

We can use the diff method to get the number of milliseconds, convert it to minutes and then ceil the result

import moment from 'moment';

let date1 = moment("2019-06-29 21:25:38+00");
console.log(date1)

let date2 = moment("2019-06-29T21:25:40.000+00:00");
console.log(date2)

// get the difference between the moments
const diff = date2.diff(date1);
console.log(diff);

// convert to minutes and ceil
const diffInMinutes = Math.ceil(diff/60000);
console.log(diffInMinutes);

Upvotes: 0

Aaron
Aaron

Reputation: 2227

It looks like you want a function that subtracts one date from another and round the difference to the nearest minute.

This can't be done with a single argument like the round function in your question; you need a function that takes both dates as arguments.

I'm not too familiar with moment, but here's a function that should work for built-in Dates.

EDIT: Per OP's comments, I've updated the function to modify d2 rather than returning diff.

    MS_IN_MINUTES = 60000;

    roundAway = (d1, d2) => {
      let diff = d2 - d1 // Difference in milliseconds (+ or -)
      let positive = diff > 0 // Whether we should add later
      diff = Math.abs(diff) // Difference in milliseconds (+)
      diff = diff / MS_IN_MINUTES // Difference in minutes (not rounded)
      diff = Math.ceil(diff) // Difference in minutes (rounded up)
      let roundedAway = d1.getTime()
      if (positive) {
          roundedAway += diff * MS_IN_MINUTES
      }
      else {
          roundedAway -= diff * MS_IN_MINUTES
      }
      d2.setTime(roundedAway)
    }
    
    let date1 = new Date("2019-06-29 21:25:38+00");
    console.log('date1:', date1)

    let date2 = new Date("2019-06-29T21:25:40.000+00:00");
    console.log('date2:', date2)

    let date3 = new Date("2019-06-29T21:24:36.000+00:00");
    console.log('date3:', date3)

    console.log('Unchanged date1:', date1);

    roundAway(date1, date2);
    console.log('Rounded date2:', date2);

    roundAway(date1, date3);
    console.log('Rounded date3:', date3);

Hope that helps!

Upvotes: 1

Maiko Morales Roman
Maiko Morales Roman

Reputation: 106

This might help you round a date to the nearest minute:

const date = moment("2019-06-29T21:59:59.000+00:00");
const seconds = date.seconds();
date.add(seconds > 0 && 60 - seconds, 's');

Upvotes: 0

Related Questions