Difference in years between dates

I want to calculate the difference in years between 2 dates. I have the current date stored in a variable using the new Date date object. The second date comes from a user input, which I am attempting to convert to a date object to be able to calculate the difference with getFullYear. However, I cannot get the value from user input into a date object. Console log returns ‘invalid date’.

Here is my latest code:

export function dob_change(event) {
 const today = new Date();
 A = $w("#dob").value;
 B = new Date(A);

 $w("#age").value = today.getFullYear() - A.getFullYear();
}

Any advice much appreciated.

In what format is the user input? Based on the input’s format, you will have to format it so that the Date object can use it. For information, see Corvid: Formatting Dates . An Internet seach will supply you with many more options.

Why not use a Datapicker ?