gpt4 book ai didi

javascript - Luxon 区间人类可读

转载 作者:行者123 更新时间:2023-12-03 16:18:18 25 4
gpt4 key购买 nike

嗨,我想以本地化的人类可读方式(例如 9 天,3 小时)表达一个 luxon 间隔。
从现在开始,我已经做到了这一点。使用此代码:

DateTime.fromISO(value).toRelative({ locale: "es" });
但是我不能使用 Interval 或 Duration 对象来达到同样的效果。
这样就完成了工作。但并不是真正的本地化。
    const start = DateTime.fromSQL("2020-06-19 11:14:00");
const finish = DateTime.fromSQL("2020-06-21 13:11:00");

const {days, hours, minutes} = Interval
.fromDateTimes(start, finish, {locale: "es"})
.toDuration(["days", "hours", "minutes"]).values;

console.log(
`${days ? days + " días " : ""} ${hours ? hours + " horas" : ""} ${
minutes ? minutes + " minutos." : ""
}`
);

最佳答案

时长 haven't any analogues of humanize() method ,所以你应该使用第三方库。例如, humanize-duration ,具有多语言支持。

const DateTime = luxon.DateTime;
const Interval = luxon.Interval;

const start = DateTime.fromSQL("2020-06-19 11:14:00");
const finish = DateTime.fromSQL("2020-06-21 13:11:00");

const formatted = Interval
.fromDateTimes(start, finish)
.toDuration()
.valueOf();

console.log(humanizeDuration(formatted))
console.log(humanizeDuration(formatted, { language: 'es' }))
console.log(humanizeDuration(formatted, { language: 'ru' }))
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/humanize-duration@3.25.1/humanize-duration.min.js"></script>

关于javascript - Luxon 区间人类可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63526184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com