【问题标题】:How to format a Date with Luxon?如何使用 Luxon 格式化日期?
【发布时间】:2021-01-11 20:02:32
【问题描述】:

使用 moment.js,您可以这样格式化日期:

const date = moment("2010-10-22T21:38:00");
const data = date.format("LL - LT") 
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js" integrity="sha512-Izh34nqeeR7/nwthfeE0SI3c8uhFSnqxV0sI9TvTcXiFJkMd6fB644O64BRq2P/LA/+7eRvCw4GmLsXksyTHBg==" crossorigin="anonymous"></script>

如何用 Luxon 做同样的事情?我试过了:

const date = luxon.DateTime.fromISO("2010-10-22T21:38:00" );
const data = luxon.DateTime.fromFormat(date, "DATETIME_SHORT")
// output => script error
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>

【问题讨论】:

    标签: javascript date luxon


    【解决方案1】:
    const date = DateTime.fromISO("2010-10-22T21:38:00")
    const humanReadable = date.toLocaleString(DateTime.DATETIME_MED);
    
    console.log(humanReadable); // =>  October 22, 9:38 PM
    

    来源:https://github.com/moment/luxon/blob/master/docs/formatting.md

    您可以在此处阅读有关可能格式的更多信息:https://github.com/moment/luxon/blob/master/docs/formatting.md#the-basics

    【讨论】:

    • 链接失效了。这是一个更新版本:github.com/moment/luxon/blob/master/docs/formatting.md
    • @TheProgrammer 谢谢,用新链接更新了我的答案。
    • Uncaught ReferenceError: DateTime is not defined
    • @oO。您是否参考了 luxon 实例?像这样:const DateTime = luxon.DateTime;const { DateTime } = require("luxon");
    猜你喜欢
    • 2021-08-30
    • 2021-12-12
    • 2022-10-13
    • 2021-08-13
    • 2021-05-20
    • 2021-02-14
    • 2021-01-22
    • 2019-09-13
    • 2021-07-28
    相关资源
    最近更新 更多