Addition and subtraction of JS implementation date, and comparison of dates

Source: Internet
Author: User

The addition and subtraction of JS implementation date
function Dateoperator (date, days, operator) {
Date = Date.replace (/-/g, "/"); Change date format
var nd = new Date (date);
nd = nd.valueof ();
if (operator = = "+") {
nd = nd + days * 24 * 60 * 60 * 1000;
} else if (operator = = "-") {
nd = nd-days * 24 * 60 * 60 * 1000;
} else {
return false;
}
nd = new Date (ND);

var y = nd.getfullyear ();
var m = nd.getmonth () + 1;
var d = nd.getdate ();
if (M <= 9) m = "0" + m;
if (d <= 9) d = "0" + D;
var CDate = y + "-" + M + "-" + D;
return cdate;
How to use
Subtraction
Alert (Dateoperator ("2015-02-28", 1, "-"));
Add
Alert (Dateoperator ("2015-02-28", 1, "+"));
}
Comparison of JS implementation dates
function Datecompare (d1, D2, Operator) {
var D1 = new Date (D1.replace (/-/g, "/"))
var D2 = new Date (D2.replace (/-/g, "/"))
if (Operator = = ">") {
return d1 > D2;
}
if (Operator = = "<") {
return D1 < D2;
}
if (Operator = = "=") {
return D1 = = D2;
}
if (Operator = = "! =") {
return D1! = D2;
}
if (Operator = = ">=") {
return D1 >= D2;
}
if (Operator = = "<=") {
return D1 <= D2;
}
How to use
Alert (Datecompare ("2015-02-28", "2015-02-27", "= ="))
}

Pro-Test effective

Addition and subtraction of JS implementation date, and comparison of dates

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.