簡體   English   中英

Antd表,如何按日期排序,包括整個時間戳?

[英]Antd table, how to sort by date including the whole timestamp?

我有這個項目

const data: Item[] = [
    {
        key: 1,
        name: 'John Brown',
        date: moment('10-10-2019').format('L'),
        address: 'New York No. 1 Lake Park',
    },
    {
        key: 2,
        name: 'Joe Black',
        date: moment('11-10-2019').format('L'),
        address: 'London No. 1 Lake Park',
    },
    {
        key: 3,
        name: 'Jim Green',
        date: moment('7-10-2019').format('L'),
        address: 'Sidney No. 1 Lake Park',
    },
    {
        key: 4,
        name: 'Jim Red',
        date: moment('8-10-2019 8:00 PM').format('L'),
        // date: moment('8-10-2019 8:00 PM').format('MMMM Do YYYY, h:mm:ss a'),
        address: 'London No. 2 Lake Park',
    },
];

如果我將該數據用於“日期”,並且我使用以下排序器,則它可以正常工作

sorter: (a, b) => moment(a.date).unix() - moment(b.date).unix()

但我希望數據更復雜一點,像這樣

date: moment('8-10-2019 8:00 PM').format('MMMM Do YYYY, h:mm:ss a')

其中評估為August 10th 2019, 8:00:00 pm

問題是,我無法使用上述排序器對數據進行排序。 我怎么能做到?

試試這個排序器:

sorter: (a, b) =>
    new Date(moment(a.date, "MMMM Do YYYY, h:mm:ss a").format("LLL")) -
    new Date(moment(b.date, "MMMM Do YYYY, h:mm:ss a").format("LLL")),

編輯

另請注意,有一個棄用警告,表明您使用的值(您提供的日期)不是公認的 RFC2822 或 ISO 格式。 您可以在moment文檔中檢查有效格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM