Date-fns on formatted date?

264 views Asked by At
const firstDate = parseISO(event.start_date); // Here I already have the formatted date 
const secondDate = parseISO(event.end_date); // Here I already have the formatted date

const distance = formatDistance(
    firstDate ,
    secondDate 
);

I need to pass the distance between these two dates that are already formatted. As it is he points me out.

this difference with all data (date and time)

here is an example of what I get at api

"start_date": "2020-09-23 11:24:14", "end_date": "2020-09-24 17:47:41",

1

There are 1 answers

0
Trott On

I don't know a whole lot about date-fns but since parseISO() returns a Date object, you can do math with the result of .getTime() to get the difference in milliseconds:

const formatDistance = (start, end) => {
  return end.getTime() - start.getTime();
}

const distance = formatDistance(
  firstDate ,
  secondDate 
);

console.log(`The two dates are ${distance}ms apart.`);