JavaScript time difference plug-in sharing for your reference. The specific content is as follows
The Html is as follows:
<html> <head> <title></title> <script src="js/TimeDifference.js" type="text/javascript"></script> <script src="js/jquery-1.10.2-min.js" type="text/javascript"></script> </head> <body> <h2> The plug-in release time: <small id="allDemo"></small> </h2> <script type="text/javascript"> $("#allDemo").text(timeDifference("2016-06-05 10:11:00")); </script> <font color="red" id="demo1Font">2016-06-03 10:20:23 </font><br> Current time difference: <strong><font color="red"><span id="timeDifferenceDemo1"></span></font></strong><br> <font color="red" id="demo2Font">2016-06-07 10:02:23 </font><br> Current time difference: <strong><font color="red"><span id="timeDifferenceDemo2"></span></font></strong> </body> <script type="text/javascript"> $(document).ready(function(){ //2016-5-3 10:20:23 var demo1Result=timeDifference($("#demo1Font").text()); $("#timeDifferenceDemo1").text(demo1Result); $("#timeDifferenceDemo2").text(timeDifference($("#demo2Font").text())); }); </script></html>The TimeDifference.js code is as follows:
/** * Function instructions: * 1. Directly call the function TimeDifference() * Return description: Return the time difference from the current * */function timeDifference(tmpTime) { var mm=1000;//1000 milliseconds represent 1 second var minute = mm * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var ansTimeDifference=0;//Record time difference var tmpTimeStamp = tmpTime ? Date.parse(tmpTime.replace(/-/gi, "/")) : new Date().getTime();//Regularly match yyyy-mm-dd H:m:s var nowTime = new Date().getTime();//Get the current timestamp var tmpTimeDifference = nowTime - tmpTimeStamp;//Calculate the difference between the timestamp and the time to be calculated if (tmpTimeDifference < 0) { //The time is exceeded, the alert("The start date is greater than the end date, the calculation fails!"); return 0; } /** * Rounding the time by the values expressed in milliseconds in each time period emphasized at the beginning. If it is 0, it will not reach * */ var DifferenceBceMonth = tmpTimeDifference / month; //Fill month round var DifferencebceWeek = tmpTimeDifference / (7 * day);//Fill week round var DifferencebceDay = tmpTimeDifference / day;//Fill day round var DifferencebceHour = tmpTimeDifference / hour;//Fill hour round var DifferencebceMinute = tmpTimeDifference / minute;//Fill minute round if (DifferebceMonth >= 1) { return tmpTime; //Fill directly return time} else if (DifferebceWeek >= 1) { ansTimeDifference= parseInt(DiferebceWeek) + "weeks ago"; } else if (DiferebceDay >= 1) { ansTimeDifference = parseInt(DiferebceDay) + "day ago"; } else if (DiferebceHour >= 1) { ansTimeDifference = parseInt(DiferebceHour) + "hours ago"; } else if (DiferebceMinute >= 1) { ansTimeDifference = parseInt(DiferebceMinute) + "minutes ago"; } else { ansTimeDifference = "Just"; } return ansTimeDifference;}The results are shown in the figure:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.