Home > Software design >  Get Minutes and Hours diff with dayjs
Get Minutes and Hours diff with dayjs

Time:06-30

I am trying to get a meeting duration to subtract the meeting end time and with meeting start time. So far with dayjs "hours" parameter, I can get the hours difference. But I also want to get minutes duration. For example, if the meeting is less than an hour, then calculate the meeting minutes.

<Info
          icon={"alarm"}
          title={
            meeting && meeting.start_time
              ? "Est. "  
                dayjs(meeting.end_time).diff(
                  dayjs(meeting.start_time),
                  "hours",
                )  
                " hour"
              : "Unavailable"
          }
        />

Right now with the following code, I am getting 0 hour when the meeting is less than an hour. Can someone help me to find out how can I get hours and minutes (when is less than hour)

CodePudding user response:

You can use the dayjs Duration plugin as follows:

const start = dayjs('2022-04-04T16:00:00.000Z');
const end = dayjs('2022-04-04T18:22:00.000Z')

const diff = dayjs.duration(end.diff(start));
const diffFormatted = diff.format('HH:mm');

console.log(diffFormatted);
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script src="https://unpkg.com/[email protected]/plugin/duration.js"></script>
<script>dayjs.extend(window.dayjs_plugin_duration)</script>

CodePudding user response:

You can get difference in Hours, Minutes, and Seconds without using dayjs too. Here's How

 const duration = Math.abs(
    new Date(meeting.end_time).getTime() - new Date(meeting.start_time).getTime()
  );

  const minutes = Math.floor(duration / 1000 / 60);
  const hours = Math.floor(duration / 1000 / 60 / 60);
  const days = Math.floor(duration / (1000 * 3600 * 24));
  • Related