typescript 验证不同格式的日期

qyzbxkaa  于 2023-01-10  发布在  TypeScript
关注(0)|答案(2)|浏览(207)

我有一个比较不同模型的函数,一个模型来自表单的初始状态(模型来自后端服务,是一个日期对象),另一个是在前端转换的时候。

function getPropertyDifferences(obj1, obj2) {
  return Object.entries(obj1).reduce((diff, [key, value]) => {
    // Check if the property exists in obj2.
    if (obj2.hasOwnProperty(key)) {
      const val = obj2[key];

      // Check if obj1's property's value is different from obj2's.
      if (val !== value) {
        return {
          ...diff,
          [key]: val,
        };
      }
    }

    // Otherwise, just return the previous diff object.
    return diff;
  }, {});
}

const a = {
  dateOfBirth: "Wed Jan 06 2021 12:00:05 GMT-0700 (Mexican Pacific Standard Time)",
  name: "test"
};
const b = {
  dateOfBirth: "2021-01-06T12:00:05.357",
  name: "test"
};

console.log(getPropertyDifferences(a, b));

如您所见,日期是相同的,但格式不同;我怎样才能在函数中验证它是相同的呢?

cvxl0en2

cvxl0en21#

理想的选择是new Date(val) !== new Date(value),但是由于第二种时间格式不包括时区,因此可以对它进行解释,您还提到您只关心日期本身,而不关心一天中的特定时间。

function getPropertyDifferences(obj1, obj2) {
  return Object.entries(obj1).reduce((diff, [key, value]) => {
    // Check if the property exists in obj2.
    
    if (obj2.hasOwnProperty(key)) {
      const val = obj2[key];

      // Check if obj1's property's value is different from obj2's.
      if (new Date(val.split(" ").slice(2,5).join(" ")) !== new Date(value.split("T")[0])) {
        return {
          ...diff,
          [key]: val,
        };
      }
    }

    // Otherwise, just return the previous diff object.
    return diff;
  }, {});
}

const a = {
  dateOfBirth: "Wed Jan 06 2021 12:00:05 GMT-0700 (Mexican Pacific Standard Time)",
  name: "test"
};
const b = {
  dateOfBirth: "2021-01-06T12:00:05.357",
  name: "test"
};


console.log(getPropertyDifferences(a, b));
q1qsirdb

q1qsirdb2#

采取的步骤:

  • 使用isNaN()函数检查属性是否为日期,该函数利用Date对象的valueOf()函数。
  • 就像你说的,你只关心日期而不关心时间;我们可以使用Date对象的toDateString()扩展方法。
function getPropertyDifferences(obj1, obj2) {
  return Object.entries(obj1).reduce((diff, [key, value]) => {
    // Check if the property exists in obj2.
    if (obj2.hasOwnProperty(key)) {
      const val = obj2[key];

      if (typeof val === "string") {
        const obj1Date = new Date(value);
        const obj2Date = new Date(val);

        // check if the values are valid dates. if not, we go through regular comparison
        if (!isNaN(obj1Date) && !isNaN(obj2Date)) {
          return obj1Date.toDateString() !== obj2Date.toDateString() ? { ...diff,
            [key]: val
          } : diff;
        }

      }

      // Check if obj1's property's value is different from obj2's.
      if (val !== value) {
        return {
          ...diff,
          [key]: val,
        };
      }
    }

    // Otherwise, just return the previous diff object.
    return diff;
  }, {});
}

const a = {
  dateOfBirth: "Wed Jan 06 2021 12:00:05 GMT-0700 (Mexican Pacific Standard Time)",
  name: "test",
  num: 1
};
const b = {
  dateOfBirth: "2021-01-06T12:00:05.357",
  name: "test",
  num: 2
};

console.log(getPropertyDifferences(a, b));

相关问题