仅用“月,年”来排序日期,在不同的浏览器中表现不同

问题描述 投票:-1回答:1

我需要对包含日期的数组进行排序,该日期包含MMM-YYY | mmmm,yyyy格式。它在跨浏览器中的排序方式不同,在chrome和Firefox / IE上按预期排序的问题不是这样!

尝试使用moment格式化日期,或dateformat但仍然相同。

我的代码:

groupProjects() {
    let dateFormat = require('dateformat');

    // const lastDate = moment("1900-01-01", 'YYYY-MM-DD').format('MMM-YYYY');
    // this gives an object with dates as keys
    groups = filteredProjects.length > 0 ? filteredProjects.reduce((groups, project) => {
        //const date = project.Properties.ModificationTime.Value.split('T')[0];
        //const  month = moment(project.Properties.ModificationTime.Value, 'YYYY-MM-DD').format('MMM');
        //console.log(project)
        if (project.Properties) {
            // const date = moment(project.Properties.ModificationTime.value, 'YYYY-MM-DD').format('MMM-YYYY');
            const date = dateFormat(project.Properties.ModificationTime.value, 'mmmm, yyyy')
            if (!groups[date]) {
                groups[date] = [];
                console.log(date)
            }
            groups[date].push(project);
        } else {
            if (!groups[/*lastDate*/ "Others"]) {
                groups[/*lastDate*/ "Others"] = [];
            }
            groups[/*lastDate*/ "Others"].push(project);
        }
        return groups;
    }, {}) : [];

    // let testGroups = this.renameProp(lastDate, 'Others', {lastDate: groups[lastDate]})

    // Edit: to add it in the array format instead
    const groupedProjects = Object.keys(groups).map((date) => {
        return {
            // key : ID(),
            date,
            // projects: groups[date].sort((a, b) => new Date(a.Properties.ModificationTime.value) > new Date(b.Properties.ModificationTime.value) ? -1 :
            //     new Date(a.Properties.ModificationTime.value) < new Date(b.Properties.ModificationTime.value ? 1 : 0))
            projects: groups[date].sort((a, b) => a.Properties && b.Properties ?
                new Date(b.Properties.ModificationTime.value) - new Date(a.Properties.ModificationTime.value) : 0)
        };
    });

    groupedProjects.sort((a, b) => b.date - a.date); //Working differently in different browsers!
    // groupedProjects.sort((a, b) => new Date(b.date) - new Date(a.date));

注意

如果我不按预期使用const date = new Date(project.Properties.ModificationTime.value)进行任何格式的排序,但是我需要按月/年分组以汇总该月的所有项目...然后开始出现问题:

javascript cross-browser momentjs date-format
1个回答
0
投票

我不知道为什么将日期格式化为具有以下格式的日期:仅月份年份,排序无法按预期方式运行,但是使用以下格式时:日月份年份,则可以正常使用!!!

最后,我放弃了,我决定通过汇总(月+年)数字来对数字进行排序,而不是对日期进行排序,就这么简单。

function sortByDate(strDateA, strDateB) {
    // console.log("A......", strDateA, "B....", strDateB)

    const a = new Date("01/".concat(strDateA)); //console.log("a: ", a)
    const b = new Date("01/".concat(strDateB)); //console.log("b: ", b)

    let r=0;
    if (a.valueOf() && b.valueOf()) {
        let x = a.getMonth() + a.getFullYear();
        let y = b.getMonth() + b.getFullYear();
        r = (x < y) ? 1 : ((x > y) ?  -1 : 0);
        //console.log(r)
    }

    return r;
}
© www.soinside.com 2019 - 2024. All rights reserved.