JSFiddle - React, Tailwind, and code Playground
HTML
<div id="time-elapsed"></div>
JavaScript
var startDateTime = new Date(2014,8,26,2,1,0,0); // YYYY (M-1) D H m s (start time and date from DB)
var startStamp = startDateTime.getTime();
var newDate = new Date();
var newStamp = newDate.getTime();
var timer;
function daysInMonth(FullYear,Month) {
return 32 - new Date(FullYear, Month, 32).getDate();
};
function updateClock() {
newDate = new Date();
newStamp = newDate.getTime();
var diff = Math.round((newStamp-startStamp)/1000);
var year = 0;
var month = 0;
var d = Math.floor(diff/(24*60*60));
diff = diff-(d*24*60*60);
var h = Math.floor(diff/(60*60));
diff = diff-(h*60*60);
var m = Math.floor(diff/(60));
diff = diff-(m*60);
var s = diff;
var ny = newDate.getFullYear();
var nm = newDate.getMonth();
var nd = newDate.getDate()-1;
d -= nd;
var ndim = daysInMonth(ny,nm);
var dim = ndim;
while(d > 0)
{
nm--;
if(nm <= 0) nm = 11;
if(nm == 11) ny--;
dim = daysInMonth(ny,nm);
if(d >= dim)
{
d -= dim;
if(month == 12)
{
month = 0;
year++;
}
else month++;
}
else
{
if(d+nd < ndim)
{
nd += d;
d = 0;
}
else
{
d -= (ndim - nd);
if(month == 12)
{
month = 0;
year++;
}
else month++;
nd = d;
d = 0;
}
}
}
var str = '';
if(year) str += year+' year(s), ';
if(month) str += month+' month(s), ';
str += nd+" day(s), "+h+" hour(s), "+m+" minute(s), "+s+" second(s) working"
document.getElementById("time-elapsed").innerHTML = str;
}
setInterval(updateClock, 1000);