JS calculates the number of days for two different dates, and the number of days for different js dates

Source: Internet
Author: User
Tags date1

JS calculates the number of days for two different dates, and the number of days for different js dates

<Span style = "font-size: 18px;"> function getDays (strDateStart, strDateEnd) {var strSeparator = "-"; // Date delimiter var oDate1; var oDate2; var iDays; oDate1 = strDateStart. split (strSeparator); oDate2 = strDateEnd. split (strSeparator); <span style = "color: # FF0000;"> var strDateS = new Date (oDate1 [0], oDate1 [1]-1, oDate1 [2]); var strDateE = new Date (oDate2 [0], oDate2 [1]-1, oDate2 [2]); </span> iDays = parseInt (Math. Abs (strDateS-strDateE)/1000/60/60/24) // convert the number of milliseconds to the number of days return iDays;} function getDay () {// startdate var dsLineTime = $ ('# dslinetime '). val (); // var deLineTime = $ ('# deLineTime '). val (); var cha = getDays (dsLineTime, deLineTime) if (deLineTime! = "Null" & deLineTime! = "" & DsLineTime! = "" & DsLineTime! = "Null") {$ ('# rangeCoef'). val (cha) ;}</span>}


Note: The reason why oDate1 [1]-1 minus 1 in the month is that the actual calculated amount in the month is 0-11, and the month on the page is 1-12.



How does js calculate the month difference between two dates?

// Two dates
Var date1 = '2017-03-26 ';
Var date2 = '2017-01-10 ';
// Split by year, month, and day
Date1 = date1.split ('-');
// Obtain the number of months
Date1 = parseInt (date1 [0]) x 12 + parseInt (date1 [1]);
// Split by year, month, and day
Date2 = date2.split ('-');
// Obtain the number of months
Date2 = parseInt (date2 [0]) x 12 + parseInt (date2 [1]);
Var m = Math. abs (date1-date2 );
Alert (m );

How does js select a date to instantly display the days of two different dates?

Suppose you can get the year, month, and day of two dates (the following uses 2011.7.16 and 2011.7.20 as an example)

// The month in Javascript starts from scratch.
// Write 7 in December August and 0 in December July and 6 in December 1...
Var day1 = (new Date). setFullYear (2011, 7, 16 );
Var day2 = (new Date). setFullYear (2011, 7, 20 );
Var number_of_days = (day2-day1)/86400000;

// Output 4
Alert (number_of_days );
// Assign the value to the input
Document. getElementById ('days'). value = number_of_days;

It's not clear how your program works, but the general approach is.

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.