Javascript date subtraction, time difference, and javascript Time Difference

Source: Internet
Author: User

Javascript date subtraction, time difference, and javascript Time Difference
// Calculate the time difference
Var from_date = new Date (from_time );
Var end_date = new Date (end_time );

Var time_different = (end_date-from_date)/86400000; // that is, 24*60*60*1000 in milliseconds

/*
If (time _ different> 365 ){
Alert ('time span cannot exceed one year ');
}
*/
The difference between the current date and time in the javascript System and the date on which the database is obtained is reduced.

You can manually write the date subtraction.
Take out the year, month, and day, And subtract them accordingly. If not, use a bid. Just like decimal subtraction, there are some changes in the borrow space here.
By day and by month.

Javascript date subtraction involves time, minute, and second

If the date is in the string format, convert the string to the date, for example:
Var s1 = new Date ("00:58:30 ");
Var s2 = new Date ("08:58:48 ");
Then get the object's millisecond representation using getTime () Subtraction
Var s3 = s2.getTime ()-s1.getTime ();
Returns the number of milliseconds of difference.
Then, based on 1 day = 24 hours = (24*60) minutes = (24*60*60) seconds = (24*60*60*1000) milliseconds
Var tianshu = s3/(24x60*60*1000 );
In this way, the number of days is different.

All of them are manually typed. Thank you for your adoption.

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.