This article describes the method of JS to calculate the number of minutes difference between two times. Share it for your reference, as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head> <title> jsCalculate the time difference minutes</title> </head> <body> <script type="text/javascript" language="javascript"> function TimeDifference() { //Define two variables time1 and time2 to save the start and end times respectivelyvar time1="2017-12-02 12:25"; var time2="2017-12-03 12:35"; //Judge whether the start time is greater than the end dateif(time1>time2) { alert("The start time cannot be greater than the end time!"); return false; } //Seave the string and get the date part "2009-12-02", use split to separate the string into an arrayvar begin1=(0,10).split("-"); var end1=(0,10).split("-"); //Recombine the split array and instance to create a new date objectvar date1=new Date(begin1[1] + - + begin1[2] + - + begin1[0]); var date2=new Date(end1[1] + - + end1[2] + - + end1[0]); //Get the difference m between two dates, in minutes//(date2-date1) Calculate the difference in milliseconds//(date2-date1)/1000 gets the difference in seconds//(date2-date1)/1000/60 gets the difference in minutesvar m=parseInt((date2-date1)/1000/60); // Add the number of hours and minutes to get the total number of minutes//(11,2) Intercept the string to get the number of hours of time//parseInt((11,2))*60 converts the number of hours into minutesvar min1=parseInt((11,2))*60+parseInt((14,2)); var min2=parseInt((11,2))*60+parseInt((14,2)); //Subtract two minutes to obtain the difference in the time part, in minutesvar n=min2-min1; //Add the difference calculated by the two parts of date and time, that is, the number of minutes after the two times is subtractedvar minutes=m+n; (minutes); } TimeDifference(); </script> </body> </html>
Running result: 1450
PS: Here are a few time and date related tools for your reference:
Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi
Online date calculator/phase difference day calculator:
http://tools./jisuanqi/datecalc
Online date day difference calculator:
http://tools./jisuanqi/onlinedatejsq
Unix timestamp conversion tool:
http://tools./code/unixtime
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript time and date operation skills》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript traversal algorithm and skills"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.