问题描述
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
);
我需要传递已经格式化的两个日期之间的距离。 就这样,他指出了我。
与所有数据(日期和时间)的差异
这是我在api上获得的一个例子
“开始日期”:“ 2020-09-23 11:24:14”, “ end_date”:“ 2020-09-24 17:47:41”,
解决方法
我对date-fns
并不了解很多,但是由于parseISO()
返回了一个Date
对象,因此您可以对.getTime()
的结果进行数学运算以获得差异以毫秒为单位:
const formatDistance = (start,end) => {
return end.getTime() - start.getTime();
}
const distance = formatDistance(
firstDate,secondDate
);
console.log(`The two dates are ${distance}ms apart.`);