0

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?

3
  • Hint: Try creating and comparing date objects with new Date(stringRepresentation) Commented Jan 9, 2023 at 16:37
  • 1
    dateOfBirth in b is ambiguous since it doesn't have any timezone information in it, so it depends on the location of the user Commented Jan 9, 2023 at 16:38
  • at the moment I do not really care about time zone, so I only want to know if 2021-01-06 are equals @Samathingamajig Commented Jan 9, 2023 at 16:52

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

It is weird that is working here, but when I try to use in code it throw errors in const obj1Date = new Date(value); Argument of type 'unknown' is not assignable to parameter of type 'string | number | Date'., and for isNaN says: Argument of type 'Date' is not assignable to parameter of type 'number'
Oh yeah, I suspect it is because your object might have properties of type numbers, and when you pass a number to the Date constructor, it returns a valid Date object. I also recommend creating a new function that encapsulates the date comparison out of this snippet. I just updated the code snippet, try that and see if it works
0

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

1 Comment

But it should no return any, because the values should be the same

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.