简体   繁体   English

仅使用“月、年”对日期进行排序,在不同的浏览器中表现不同

[英]Sorting a date with "Month, Year" only, behaving differently in different browsers

I need to sort an array that includes a date with MMM-YYY |我需要使用 MMM-YYY 对包含日期的数组进行排序 | mmmm, yyyy format. mmmm, yyyy 格式。 The problem that it's sorting differently cross browsers, it's sorted as expected on chrome and on Firefox/IE it's not!问题是它在跨浏览器上的排序方式不同,它在 chrome 上按预期排序,而在 Firefox/IE 上却不是!

Tried using moment for formatting the date, or dateformat but still the same.尝试使用moment格式化日期或dateformat但仍然相同。

My code:我的代码:

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));

Note笔记

If I use const date = new Date(project.Properties.ModificationTime.value) without any formatting its sorting as expected, but I need to group by Month/Year to aggregate all projects from that month together... and there where the problem starts to happen:如果我使用const date = new Date(project.Properties.ModificationTime.value)没有按预期对其排序进行任何格式化,但我需要按月/年分组以将该月的所有项目聚合在一起......问题在哪里开始发生:

I couldn't figure out why when formatting the date to format that has: Month Year only, the sorting is not working as expected, but when using a format with: Day Month Year, it works!!!!我无法弄清楚为什么将日期格式化为仅具有以下格式的日期:仅月年,排序未按预期工作,但是当使用具有以下格式的格式时:日月年,它有效!!!!

At the end I gave up and instead of sorting dates i decided to sort numbers by summarizing the (month + year) number, simple as that.最后我放弃了,我决定通过总结(月 + 年)数字来对数字进行排序,而不是对日期进行排序,就这么简单。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM