JSFiddle - React, Tailwind, and code Playground
HTML
<table>
<thead>
<tr>
<td>Name</td>
<td>Startdate</td>
<td>Date in years</td>
</tr>
</thead>
<tbody>
<tr>
<td>Employee #1</td>
<td>1992/07/07 18:00:00</td>
</tr>
<tr>
<td>Employee #2</td>
<td>1990/01/01 18:00:00</td>
</tr>
<tr>
<td>Employee #3</td>
<td>2013/01/01 18:00:00</td>
</tr>
<tr>
<td>Employee #4</td>
<td>2012/01/01 18:00:00</td>
</tr>
</tbody>
</table>
CSS
td { padding:2px; border:1px solid black; }
JavaScript
var rows = document.querySelectorAll("tbody tr"); // Get all the rows
for(var i = 0; i < rows.length; i++) {
// For each row, get the start date
var startdate = rows[i].cells[1].innerText,
years = calculateYears(startdate);
// Create a DOM element with the result, and add it to the table
var td = document.createElement("td"),
result = document.createTextNode(years);
td.appendChild(result);
rows[i].appendChild(td);
}
// Years calculation
function calculateYears(startdate) {
var dateObj1 = new Date( startdate );
var dateObj2 = new Date();
//get difference in milliseconds
var diffMilliseconds = dateObj1.getTime() - dateObj2.getTime();
//make the difference positive
if( diffMilliseconds < 0 ) diffMilliseconds *= -1;
//convert milliseconds to hours
var diffYears = ( diffMilliseconds / 1000 ) / 60 / 60 / 24 / 365;
//print on console
var num = diffYears;
num = Math.floor(num);
if (num > 1)
{
return num + ' years';
}
else if (num < 1)
{
return 'less than one year';
}
else
{
return num + ' year';
}
}