Luxon 间隔人类可读

Luxon interval human readable

您好,我想以本地化的人类可读方式(例如 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, so you should use a third-party library. For example, 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>