Friends, hello.

From the partners API comes the date of birth of the user in the format DD.MM.YYYY or DD.MM (if the year of birth is hidden). Based on these data, you need to determine the age of the person.

Help with the function. Something like this would probably come up, but new Date () does not accept the date in DD.MM.YYYY format.

function get_current_age(date) { return ((new Date().getTime() - new Date(date)) / (24 * 3600 * 365.25 * 1000)) | 0; } 
  • Are there already ready libraries that have implemented this type of moment, or do you really need something of your own? - zhenyab
  • libraries it is not advisable to use - Sergey
  • Date.TryParse () tried? Link here - alexoander
  • In principle, you can simply add a check to the beginning of the above function: var d = date.split ('.'); if (d [2]! = "undefined") date = d [2] + '.' + d [1] + '.' + d [0] else return false; But some govnokod turns. - Sergey
  • 2
    @Igor, 2016 or 46 :-) - Grundy

1 answer 1

 function get_current_age(date) { var d = date.split('.'); if( typeof d[2] !== "undefined" ){ date = d[2]+'.'+d[1]+'.'+d[0]; return ((new Date().getTime() - new Date(date)) / (24 * 3600 * 365.25 * 1000)) | 0; } return 0; }