简体   繁体   English

如何使用 Luxon 从 UTC 日期获取相对时间?

[英]How to get relative time from a UTC date using Luxon?

I store my creation dates for posts as DateTime.UtcNow() with my .net core api.我将我的帖子创建日期存储为 DateTime.UtcNow() 与我的 .net 核心 api。

If I use Moment.js it is as simple as :如果我使用 Moment.js,它很简单:

convertedDate(date: Date) {
    return moment.utc(date).fromNow();
}

This returns something like 22 hours ago or 19 minutes ago.这会返回 22 小时前或 19 分钟前的内容。

I was unable to really find information on how to work with the luxon library.我无法真正找到有关如何使用 luxon 库的信息。

Any tips?有小费吗?

It looks like Luxon doesn't support this behavior because it doesn't have access to internationalized strings.看起来 Luxon 不支持这种行为,因为它无法访问国际化的字符串。 More information/alternatives can be found here.可以在此处找到更多信息/替代方案。

https://github.com/moment/luxon/issues/364 https://github.com/moment/luxon/issues/364

First include library or install, say like首先包括库或安装,说喜欢

<script src="https://cdn.jsdelivr.net/npm/luxon@1.10.0/build/global/luxon.js"></script>

Then use following format to get proper output:然后使用以下格式获得正确的输出:

const DateTime = luxon.DateTime;
console.log(DateTime.local().plus({ days: 1 }).toRelative()); // in 23 hours
console.log(DateTime.local().minus({ days: 2 }).toRelative({ unit: "hours" })); //48 hours ago
console.log(DateTime.local().toObject()); // year: 2020 month: 2 day: 25 hour: 0 minute: 4 second: 20 millisecond: 764
console.log(DateTime.local(2014, 7, 13).toSQL({ includeZone: true })); // 2014-07-13 00:00:00.000 Asia/Dhaka

Besides, If you want to explore more to learn other methods please read documentation for Luxon.此外,如果您想探索更多以学习其他方法,请阅读 Luxon 的文档。

https://moment.github.io/luxon/index.html

You can use toRelative() luxon function, it works same way.您可以使用toRelative() luxon函数,它的工作方式相同。 For example:例如:

DateTime.local().minus({ days: 2 }).toRelative() //=> "2 days ago"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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