Create two Date objects - a birthday and now. To ensure that you correctly get a date object from a text line like yours, you first need to bring it to the ISO 8601 format . The easiest way is to replace the space with the letter "T". But this date will be in the UTC time zone, you need to adjust it by adding minutes from getTimezoneOffset (). For example, the date "2014-09-12 00:00:00" Moscow time, it is September 11, 21:00 UTC. When comparing from “now”, we will also take time according to UTC - getUTCMonth (), etc.
Having received two dates, let's compare their months and days. If greater accuracy is required, then also hours, minutes, seconds:
var sqlDT = "2014-09-12 00:00:00"; var BD = new Date( sqlDT.replace(' ','T')); var Now = new Date(); BD.setMinutes( BD.getMinutes() + Now.getTimezoneOffset()); // в UTC if( BD.getMonth() === Now.getUTCMonth() && BD.getDate() === Now.getUTCDate() ) { // ура! День рождения! var y = Now.getUTCFullYear() - BD.getFullYear(); // сколько полных лет }
An alternative option is to connect the library to work with dates and times - MomentJS , and find the appropriate methods in it.