JavaScript date subtracted to get the second date

Source: Internet
Author: User

The JavaScript date is subtracted, and then the second date is obtained. In peacetime encounter time plus and minus the probability is relatively large, the following JS function can be completed a specified number of days minus a certain number of days, calculated another date, in many places can be used. You need to look at the function code, which has a usage example, followed by a parameter description.

Example

The code is as follows Copy Code
var sdate= "2006-06-06";
var edate= "2007-07-07";
var sArr = Sdate.split ("-");
var Earr = Edate.split ("-");
var srdate = new Date (sarr[0], sarr[1], sarr[2]);
var erdate = new Date (earr[0], earr[1], earr[2]);
var result = (erdate-srdate)/(24*60*60*1000);

This result is the number of days between two dates.

Example 2

The code is as follows Copy Code

<script language=javascript>
function cc (DD,DADD)
{
If you are more considerate, you can add error handling code here
var a = new Date (DD)
A = A.valueof ()
A = A-dadd * 24 * 60 * 60 * 1000
A = new Date (a)
Alert (a.getfullyear () + "year" + (A.getmonth () + 1) + "month" + a.getdate () + "Day")
}
Call Method:
CC ("10/11/2016", 2)
</script>

Parameter description: This function involves two parameters: DD: the date to be calculated; Dadd: The number of days to subtract, such as 2 days, 3 days.

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.