score:1

Accepted answer
import { useEffect, useState } from 'react';
import moment from 'moment';

import "./styles.css";

const dates = [moment('08-09-2021'), moment('08-16-2021')];

function workday_count(start,end) {
  const first = start.clone().endOf('week');
  const last = end.clone().startOf('week');
  const days = last.diff(first,'days') * 5 / 7;
  const wfirst = first.day() - start.day();
  if(start.day() === 0) --wfirst;
  const wlast = end.day() - last.day();
  if(end.day() === 6) --wlast;
  return wfirst + Math.floor(days) + wlast;
} // 

export default function App() {
  const [diff, setDiff] = useState(0);

  useEffect(() => {
    if (dates && dates.length > 1) {
      const startDate = dates[0];
      const endDate = dates[1];

      
      if (startDate && endDate) {
        const difference = workday_count(startDate.startOf('day'), endDate.startOf('day'));
        setDiff(difference); 
      }
    }
  }, []);
  return (
    <div className="App">
      <h1>Diff: {diff}</h1>
    </div>
  );
}


Related Query

More Query from same tag