How to determine the exact difference in years between the current date and the date in the format "0000-00-00 00:00:00". In PHP, this is solved simply:

DateTime::createFromFormat("Ymd H:i:s", '2014-09-12 00:00:00') ->diff(new DateTime('now')) ->y; 

Is there an elegant solution like this in Java Script?

  • using a library like momentjs can do almost the same. And you can just calculate - the date difference is the number of milliseconds - it remains only to translate milliseconds per year - Grundy
  • @Grundy in this and complexity. up to days and weeks, everything translates simply, but to correctly translate into months and years with a simple formula is not enough. - Dmitry Maslennikov
  • @Grundy would not want to connect a separate library for the sake of a single function. - Dmitry Maslennikov
  • It's easier, after all, to connect the library. Or see how it is implemented in it - Grundy

3 answers 3

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.

  • when converted to UTC, the difference is incorrectly processed. for example, between '2017-03-29 21:00:00' and '2015-03-29 19:00:00' the result of the calculations will be 1, not 2 (correct if used without formatting). - Dmitry Maslennikov
  • if that time zone is Moscow, +3. - Dmitry Maslennikov
  • @DmitryMaslennikov incorrect result because you, apparently, crookedly corrected the code. Here is the fiddle where two any text dates are compared - there it turns out 2. And here is the fiddle , which compares '2015-03-29 19:00:00' and now (I'm in UTC + 3) - it turns out 2. - Sergiks
  • I don’t understand what’s the matter then, here’s fiddle with my code rewritten in the style you recommended: jsfiddle.net/d0x924rw/4 - Dmitry Maslennikov
  • by date everything works correctly there, but the clock already gives an incorrect result. try entering variables +/- hour from the current time. - Dmitry Maslennikov

If by exact difference you meant the meaning of the year, and not years according to the number of the past months, then you can:

 var unix_date = "2016-01-01 00:00:00"; var jsYear = new Date(unix_date).getFullYear(); var currentYear = new Date().getFullYear(); var yearsDifference = currentYear - jsYear; console.log(yearsDifference); 

  • No, it is precisely the second option (by the number of the past months, but accuracy to the second is needed). Found a solution, see below - Dmitry Maslennikov

The final code. At the entrance there is a date in the format '0000-00-00 00:00:00', at the exit the number of full years from the current date:

 function age_count(date) { // now var now = new Date(); now.setMinutes(now.getMinutes() - now.getTimezoneOffset()); now = now.toISOString().substr(0, 19).replace('T',' '); // calculate var age = now.substr(0, 4) - date.substr(0, 4); if(now.substr(5) < date.substr(5)) --age; // output return age; }