<! DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> var pretime = "2018-04-07 14:38:16 "; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; // This is called getDate(pretime) the first time the page is opened; SetInterval (function() {getDate(pretime); }, 60000); function getDate(dateTimeStamp){ if(dateTimeStamp==undefined){ return false; }else{ dateTimeStamp = dateTimeStamp.replace(/\-/g, "/"); var sTime = new Date(dateTimeStamp).getTime(); Var now = new Date().gettime (); Var diffValue = now-stime; If (diffValue < 0){console.log(" End date cannot be less than start date!" ); } var monthC =diffValue/month; var weekC =diffValue/(7*day); var dayC =diffValue/day; var hourC =diffValue/hour; var minC =diffValue/minute; If (monthC>=1){console.log(parseInt(monthC) + "months ago "); } else if(weekC>=1){console.log(parseInt(weekC) +" weeks ago ")} else if(dayC>=1){console.log(parseInt(dayC) +" days ago ")} else If (hourC>=1){console.log(parseInt(hourC) +" minutes ago ")}else if(minC>=1){console.log(parseInt(minC) +" minutes ago ")}else{ The console. The log (" just ")}}} < / script > < / body > < / HTML > duplicate codeCopy the code