JavaScript calculates two time difference

Source: Internet
Author: User

In fact, JavaScript calculates the time difference in a very simple way, if it is the default date () type, the direct subtraction is the difference between milliseconds

var D1 = new Date (' 2016/03/28 10:17:22 '), var d2 = new Date (' 2016/03/28 11:17:22 '), Console.log (parseint (D2-D1));//Two time difference Number of milliseconds Console.log (parseint (D2-D1)/1000),//Two time difference in seconds Console.log (parseint (D2-D1)/1000/60);//two time difference in minutes console.log ( parseint (D2-D1)/1000/60);//Two hours of difference between time

If you do not get the date type, but rather the string format "2016-03-28 10:27:00", then you need to convert the string to a date type first.

var t1 = "2016-03-28 10:27:00"; var D1 = T1.replace (/\-/g, "/"); var date1 = new Date (D1);


The above-mentioned date1 is a date type. Can be calculated, as long as a simple encapsulation, you can assemble any event type conversion function.

For example: The input string gets the number of seconds difference:

<!doctype html>

JavaScript calculates two time difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.