issameorbefore日期fns的实现

v6ylcynt  于 2021-09-23  发布在  Java
关注(0)|答案(6)|浏览(525)

由于不推荐使用moment,我决定将整个项目中的moment隐藏起来,并考虑使用datefns作为替代。当我在查看代码文件时,我看到一行字,上面写着,

moment(date1).isSameOrBefore(date2, year);

我开始寻找类似于该函数的功能,但不幸的是,date fns没有任何东西可以执行基于精度的比较(即:年、月或日期比较)?有人能推荐一些date fns的解决方案吗?

kpbwa7wx

kpbwa7wx1#

只要针对相关力矩函数进行测试,就应该能够创建插入式替换。
我们可以寻找相关的startof程序来比较 unit 指定,如果缺少,也会引发错误:

function isSameOrBefore(date1, date2, unit = '') {
    if (unit) {
        const procName = `startOf${unit[0].toUpperCase()}${unit.slice(1)}`;
        if (!dateFns[procName]) { 
            throw new Error(`isSameOrBefore: Unsupported unit: ${unit}`);
        }
        date1 = dateFns[procName](date1);
        date2 = dateFns[procName](date2);
    }
    return dateFns.isBefore(date1, date2) || dateFns.isEqual(date1, date2);
}

// Test against various inputs
const dts = ['2010-10-20 01:00', '2010-10-20 00:00', '2010-10-20 00:59:00', '2010-10-01', '2010-09-01', '2009-09-01'];
let units = ['year', 'month', 'day', 'hour', 'minute'];
let dt1 = '2010-10-20 01:00'
let allTests = [];
for(let dt2 of dts) {
    for(let unit of units) {
        // Ensure we get the same answer as moment().isSameOrBefore
        const testPassed = isSameOrBefore(dt1, dt2, unit) === moment(dt1).isSameOrBefore(dt2, unit)
        allTests.push(testPassed);
        console.log(`isSameOrBefore(${dt1}, ${dt2}, ${unit}):`, isSameOrBefore(dt1, dt2, unit));
        console.log(`isSameOrBefore(${dt1}, ${dt2}, ${unit}): Test passed:`, testPassed ? "TRUE": "FALSE")
    }
}
console.log("All tests passed:", allTests.every(res => res) ? "TRUE": "FALSE")
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js" integrity="sha512-F+u8eWHrfY8Xw9BLzZ8rG/0wIvs0y+JyRJrXjp3VjtFPylAEEGwKbua5Ip/oiVhaTDaDs4eU2Xtsxjs/9ag2bQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
qc6wkl3g

qc6wkl3g2#

使用 startOfSOMETHING() 您可以使用分钟、小时、日、月ecc检查日期是否早于或相同,以进行比较。

const dateBefore = dateFns.startOfHour(new Date(1988, 9, 26, 05, 35));
const dateSame = dateFns.startOfHour(new Date(1988, 9, 26, 11, 55));
const dateAfter = dateFns.startOfHour(new Date(1988, 9, 26, 16, 50));

const limit = dateFns.startOfHour(new Date(1988, 9, 26, 11, 55));

// dateBefore < limit => is before
if (dateFns.isBefore(dateBefore, limit) || dateFns.isEqual(dateBefore, limit)) console.log(dateFns.format(dateBefore), 'is before');
else console.log('is not before');

// dateSame === limit => is same
if (dateFns.isBefore(dateSame, limit) || dateFns.isEqual(dateSame, limit)) console.log(dateFns.format(dateSame), 'is same');
else console.log('is not same');

// dateAfter > limit =>  is after
if (dateFns.isBefore(dateAfter, limit) || dateFns.isEqual(dateAfter, limit)) console.log('you mustnt see that message ');
else console.log(dateFns.format(dateAfter), 'is after');
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.28.5/date_fns.min.js"></script>

在我看来,这个库太糟糕了,我更喜欢day.js,它与moment.js非常相似,或者用 new Date() javascritp的。

v9tzhpje

v9tzhpje3#

使用 getYear 首先获取两个日期的“年”部分,然后比较两个日期。
比如:

const date1 = new Date(2021, 6, 20);
const date2 = new Date(2021, 7, 2);
const date3 = new Date(2020, 1, 15);
console.log(dateFns.getYear(date1) <= dateFns.getYear(date2));
console.log(dateFns.getYear(date1) <= dateFns.getYear(date3));
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.28.5/date_fns.min.js"></script>
bvjveswy

bvjveswy4#

你可以用 differenceInYears :

const date1 = new Date(2021, 6, 20);;
const date2 = new Date(2021, 7, 2);

function isSameOrBefore(date1, date2) {
  return dateFns.differenceInYears(date1, date2) <= 0;
}

console.log(isSameOrBefore(date1, date2));

console.log(isSameOrBefore(date1, new Date('2022-06-20')));

console.log(isSameOrBefore(date1, new Date('2019-06-20')));
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.28.5/date_fns.min.js"></script>
js81xvg6

js81xvg65#

import { isBefore, isEqual } from 'date-fns';

isSameOrBefore(date1: Date, date2: Date, property?: string) {
  switch (property) {
    case 'full':
      return isBefore(date1, date2) || isEqual(date1, date2);
    case 'year':
      return date1.getFullYear() <= date2.getFullYear();
    case 'month':
      return (
        isBefore(date1.getFullYear(), date2.getFullYear()) ||
        (isEqual(date1.getFullYear(), date2.getFullYear()) &&
          date1.getMonth() <= date2.getMonth())
      );
    case 'date':
      return (
        isBefore(date1.getFullYear(), date2.getFullYear()) ||
        (isEqual(date1.getFullYear(), date2.getFullYear()) &&
          date1.getMonth() < date2.getMonth()) ||
        (isEqual(date1.getFullYear(), date2.getFullYear()) &&
          isEqual(date1.getMonth(), date2.getMonth()) &&
          date1.getDate() <= date2.getDate())
      );
    default:
      return compareDesc(date1, date2);
  }
}

您可以为一天、一刻……添加更多实现。。。。

ibps3vxo

ibps3vxo6#

让我也发布一个答案。它不需要您导入整个日期fns

import { endOfDay, endOfWeek, endOfMonth, endOfYear } from 'date-fns';

export function isSameOrBefore(date1, date2, unit) {
    let method;
    switch (unit) {
        case 'day':
            method = endOfDay;
            break;
        case 'week':
            method = endOfWeek;
            break;
        case 'month':
            method = endOfMonth;
            break;
        case 'year':
            method = endOfYear;
            break;
        default:
            break;
    }
    const startTimestamp = method ? method(date1): date1;
    return dateFns.isBefore(startTimestamp, date2) || dateFns.isEqual(startTimestamp, date2);
}

相关问题