Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
functiongetMonthDays(nmonth,nyear){switch(nmonth){case0:case2:case4:case6:case7:case9:case11:return31;case3:case5:case8:case10:return30;case1:if(nyear%4==0)return29;elsereturn28;default:return0;}}functiongetAgoTime(gdate,cdate){vardaysago=0;varmonthago=0;varyearago=0;if(cdate.getMonth()<gdate.getMonth()){yearago=cdate.getFullYear()-gdate.getFullYear()-1;}elseif(cdate.getMonth()==gdate.getMonth()&&cdate.getDate()<gdate.getDate()){yearago=cdate.getFullYear()-gdate.getFullYear()-1;}else{yearago=cdate.getFullYear()-gdate.getFullYear();}if(cdate.getMonth()<=gdate.getMonth()&&cdate.getDate()<gdate.getDate()){monthago=cdate.getMonth()-gdate.getMonth()+12-1;}elseif(cdate.getMonth()>gdate.getMonth()&&cdate.getDate()<gdate.getDate()){monthago=cdate.getMonth()-gdate.getMonth()-1;}elseif(cdate.getMonth()<gdate.getMonth()){monthago=cdate.getMonth()-gdate.getMonth()+12;}else{monthago=cdate.getMonth()-gdate.getMonth();}if(cdate.getDate()>=gdate.getDate()){daysago=cdate.getDate()-gdate.getDate();}else{daysago=cdate.getDate()-gdate.getDate()+getMonthDays(gdate.getMonth(),gdate.getYear());}varagostr="";if(yearago==0&&monthago==0&&daysago==0){agostr="Today";}else{if(yearago>1)agostr+=yearago+" years ";elseif(yearago==1)agostr+=yearago+" year ";if(monthago>1)agostr+=monthago+" months ";elseif(monthago==1)agostr+=monthago+" month ";if(daysago>1)agostr+=daysago+" days ";elseif(daysago==1)agostr+=daysago+" day ";agostr+="ago";}returnagostr;}vargdate=newDate("28 February 2019");varcdate=newDate("27 March 2019");alert(getAgoTime(gdate,cdate));