How to use 'javascript age calculation' in JavaScript

Every line of 'javascript age calculation' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
87function calcAges() {
88 var HOUR = 60 * 60 * 1000;
89 var DAY = HOUR * 24;
90 var WEEK = DAY * 7;
91 var MONTH = DAY * 30;
92 var YEAR = DAY * 365;
93
94 var elts = document.getElementsByTagName("time");
95 var elt;
96 for (var i = 0; elt = elts[i]; i++) {
97 elt.title = elt.textContent || elt.innerHTML;
98 var iso = elt.getAttribute("datetime");
99 var date = new Date(iso);
100 var now = new Date();
101 var diff = now.getTime() - date.getTime();
102 var str = elt.innerHTML;
103 if (diff > YEAR) str = toAge(diff / YEAR, "năm");
104 else if (diff > MONTH) str = toAge(diff / MONTH, "tháng");
105 else if (diff > WEEK) str = toAge(diff / WEEK, "tuần", true);
106 else if (diff > DAY) str = toAge(diff / DAY, "ngày", true);
107 else if (diff > HOUR) str = toAge(diff / HOUR, "tiếng", true);
108 else str = "Mới bây giờ";
109 elt.innerHTML = str;
110 }
111 }
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code


Related snippets