[英]Difference between current time and UTC time format in javascript with Date-fns
I am using newsapi.org
to get news data and date-fns library .我正在使用
newsapi.org
来获取新闻数据和date-fns 库。 How can I get difference between PublishedTime of article which is in UTC+000 (eg: 2020-09-13T15:50:00Z) and current datetime ?如何获得UTC+000 (例如:2020-09-13T15:50:00Z)和当前日期时间的文章的PublishedTime之间的差异? What i have tried:
我尝试过的:
import { formatDistance } from "date-fns";
timeDistance = (time) => {
return formatDistance(new Date(), time);
};
where time is a string: eg: "2020-09-13T15:50:00Z"其中时间是一个字符串:例如:“2020-09-13T15:50:00Z”
Expected result: something like 10 minutes ago, 1 hour ago etc.预期结果:例如 10 分钟前、1 小时前等。
This should work:这应该有效:
import formatDistance from 'date-fns/formatDistance';
timeDistance = (time) => {
return formatDistance(new Date(), new Date(time));
};
console.log(timeDistance('2020-09-13T15:50:00Z'));
Note: make sure you import formatDistance
from date-fns/formatDistance
to make code splitting easier注意:确保导入
formatDistance
从date-fns/formatDistance
使代码更容易分裂
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.