1 回答
TA贡献1828条经验 获得超3个赞
这种方法没有得到很好的支持。我可以使用相对时间格式吗?
您可以做的是使用一个包来模拟您需要的功能,无论浏览器是什么。
我找到了您可以使用的相对时间格式npm 包。
安装
npm install 相对时间格式 --save
用
import RelativeTimeFormat from "relative-time-format"
import en from "relative-time-format/locale/en.json"
RelativeTimeFormat.addLocale(en)
// Returns "2 days ago"
new RelativeTimeFormat("en", {
style: "long" // "long" is the default. Other options: "short", "narrow".
}).format(-2, "day")
要检查浏览器是否兼容:
if (Intl === void 0 || typeof Intl.RelativeTimeFormat !== 'function') {
// Browser not compatible
}
选择 :
const rtf = (Intl &&
Intl.RelativeTimeFormat &&
new Intl.RelativeTimeFormat('en', {
style: 'short',
numeric: "auto",
})) || false;
if (!rtf) {
// Not compatible
}
添加回答
举报