I have a function that compares different models, one model is from the initial state of the form(the model comes from the backend service, which is a date object), and the other one is when it is converted in the front end.
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));
As you can see, the dates are the same but in different formats; how can I validate in the function that it is the same?
CodePudding user response:
The ideal option would be new Date(val) !== new Date(value)
, but since the second time format doesn't include a timezone it is open to interpretation. You also mentioned that you only care about the date itself, not a specific time of day.
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));
CodePudding user response:
Steps taken:
- Check if the property is a date using the isNaN() function, which utilizes the valueOf() function of the Date object.
- As you said, you only care about the date without the time; we can use toDateString() extension method of the Date object.
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));