最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get relative time from a UTC date using Luxon? - Stack Overflow

programmeradmin4浏览0评论

I store my creation dates for posts as DateTime.UtcNow() with my core api.

If I use Moment.js it is as simple as :

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

This returns something like 22 hours ago or 19 minutes ago.

I was unable to really find information on how to work with the luxon library.

Any tips?

I store my creation dates for posts as DateTime.UtcNow() with my core api.

If I use Moment.js it is as simple as :

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

This returns something like 22 hours ago or 19 minutes ago.

I was unable to really find information on how to work with the luxon library.

Any tips?

Share Improve this question asked Feb 24, 2020 at 17:15 PeteyPetey 3,3573 gold badges17 silver badges28 bronze badges 3
  • are you asking how to use Luxon library or else? – Abu Sufian Commented Feb 24, 2020 at 17:20
  • Preferably how to implement the same method using Luxon library. @AbuSufian – Petey Commented Feb 24, 2020 at 17:22
  • in Luxon you have to use 'toRelative()' instead of 'fromNow()'. you can go through this documentation: github./moment/luxon/blob/master/docs/moment.md – Abu Sufian Commented Feb 24, 2020 at 17:34
Add a ment  | 

3 Answers 3

Reset to default 3

You can use toRelative() luxon function, it works same way. For example:

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

It looks like Luxon doesn't support this behavior because it doesn't have access to internationalized strings. More information/alternatives can be found here.

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

First include library or install, say like

<script src="https://cdn.jsdelivr/npm/[email protected]/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.

https://moment.github.io/luxon/index.html
发布评论

评论列表(0)

  1. 暂无评论