对格式不正确的日期进行排序



我从excel表中获得输入,但日期不是可以操作的正确ISO格式,即该列看起来如下,我试图通过使用javascript日期和时刻将其转换为正确的日期来对其进行排序,但我没有得到正确的输出,我可以对此发表另一种意见。

  • 输入["28/02/23","31/12/21","31/05/21","30/09/23","31/10/22","30/09/21","30/06/23","31/05/22","30/04/21","31/07/21","30/06/21","28/02/22","31/12/22","30/06/22","31/08/21","30/04/22","31/10/20","31/08/22","31/07/22","31/05/23","31/01/23","30/04/23","30/09/22","28/02/21","30/11/20","30/11/21","31/01/21","31/03/23","31/01/22","31/07/23","31/12/20","31/03/21","31/08/23","30/11/22","31/10/21","31/03/22","30/09/20"]

  • 方法我写

    dateCompare = (d1, d2) => {
    let d1Component = d1.toString().split('/')
    , d2Component = d2.toString().split('/')
    , rD1 = new Date(parseInt(20 + d1Component[2]), d1Component[1] - 1, d1Component[0])
    , rD2 = new Date(parseInt(20 + d2Component[2]), d2Component[1] - 1, d2Component[0])
    return moment(rD1).isAfter(moment(rD2))
    }
    
  • 我从上面的方法得到的输出是["30/09/35", "28/02/23", "31/05/21", "31/12/21", "31/10/22", "30/09/21", "30/09/20", "31/05/22", "30/04/21", "31/07/21", "30/06/21", "28/02/22", "31/12/22", "30/06/22", "31/08/21", "30/04/22", "31/10/20", "31/08/22", "31/07/22", "31/03/22", "31/01/23", "31/10/21", "30/09/22", "28/02/21", "30/11/20", "30/11/21", "31/01/21", "30/11/22", "31/01/22", "31/03/21", "31/12/20", "31/01/37", "31/07/23", "31/03/23", "30/04/23", "31/05/23", "30/06/23", "30/04/37", "31/10/23", "31/08/23", "30/09/23", "30/06/37", "29/02/24", "30/11/23", "31/12/23", "31/01/24", "31/08/37", "31/07/24", "31/03/24", "30/04/24", "31/05/24", "30/06/24", "30/11/37", "31/01/25", "31/08/24", "30/09/24", "31/10/24", "30/11/24", "31/12/24", "31/03/38", "31/08/25", "28/02/25", "31/03/25", "30/04/25"],我认为它根本没有以任何方式排序。

比较函数应返回负数表示小于,返回正数表示大于,返回0表示等于,而不是true/false。此外,由于您已经在使用moment,因此也可以使用它来解析日期:

function dateCompare(d1, d2) {
const d1Parsed = moment(d1, "DD/MM/YY");
const d2Parsed = moment(d2, "DD/MM/YY");
if (d1Parsed.isBefore(d2Parsed)) {
return -1;
} else if (d1Parsed.isSame(d2Parsed)) {
return 0;
} else {
return 1;
}
}

或者,取它们的时间戳的差(如果d1Parsedd2Parsed之前,那么它的时间戳就会更小,所以得到的减法会是负的,如果它们相等,那么它会是0,如果它更大,那么它就会是正的,这与前一个代码相同(:

function dateCompare(d1, d2) {
const d1Parsed = moment(d1, "DD/MM/YY");
const d2Parsed = moment(d2, "DD/MM/YY");
return d1Parsed.valueOf() - d2Parsed.valueOf();
}

您可以使用映射和转换日期,而不是对它们进行排序。

const dates = ["28/02/23", "31/12/21", "31/05/21", "30/09/23", "31/10/22", "30/09/21", "30/06/23", "31/05/22", "30/04/21", "31/07/21", "30/06/21", "28/02/22", "31/12/22", "30/06/22", "31/08/21", "30/04/22", "31/10/20", "31/08/22", "31/07/22", "31/05/23", "31/01/23", "30/04/23", "30/09/22", "28/02/21", "30/11/20", "30/11/21", "31/01/21", "31/03/23", "31/01/22", "31/07/23", "31/12/20", "31/03/21", "31/08/23", "30/11/22", "31/10/21", "31/03/22", "30/09/20"];
const convertedDates = dates.map(d => {
var day = d.split("/")[0];
var month = d.split("/")[1];
var year = d.split("/")[2];
return "20" + year + '-' + ("0" + month).slice(-2) + '-' + ("0" + day).slice(-2);
})
.sort();
;
console.log(convertedDates);

在不使用第三方库的情况下,您可以将客户表日期转换为符合ISO 8601的日期字符串,并使用String#localeCompare

const
getISO = string => string.replace(/(dd)/(dd)/(dd)/, '20$3-$2-$1'),
data = ["28/02/23", "31/12/21", "31/05/21", "30/09/23", "31/10/22", "30/09/21", "30/06/23", "31/05/22", "30/04/21", "31/07/21", "30/06/21", "28/02/22", "31/12/22", "30/06/22", "31/08/21", "30/04/22", "31/10/20", "31/08/22", "31/07/22", "31/05/23", "31/01/23", "30/04/23", "30/09/22", "28/02/21", "30/11/20", "30/11/21", "31/01/21", "31/03/23", "31/01/22", "31/07/23", "31/12/20", "31/03/21", "31/08/23", "30/11/22", "31/10/21", "31/03/22", "30/09/20"];
data.sort((a, b) => getISO(a).localeCompare(getISO(b)));
console.log(data);

相关内容

  • 没有找到相关文章

最新更新