12

嗨,我想以本地化的人类可读方式(例如 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." : ""
        }`
    );
4

1 回答 1

15

Duration没有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>

于 2021-01-10T08:49:49.497 回答