简体   繁体   中英

Date-fns on formatted date?

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 an example of what I get at api

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

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.`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM