JS: Contract-known starting date, age, automatically calculated due date

Source: Internet
Author: User

Dateaddyear (' 2016-01-01 ', ' 3 ');//return: 2018-12-31

Browser: Ie11,ff 46.0.1 (Success) 360v8.1 (haste mode, success)

Browser: 360v8.1 (compatibility mode, failed)

One. Code:

Date: Day string yyyy-mm-dd, such as: 2016-02-14
Years: Year, positive integer string
Returns the date string yyyy-mm-dd, such as: 2016-02-14
function Dateaddyear (date, years) {
var now = new Date (date);
var intyear = now.getfullyear () + parseint (years);
var intmonth = now.getmonth () + 1; The normal month,
var intday = Now.getdate ()-1; Date-1
if (Intday = = 0) {
intmonth--; One months reduced
if (Intmonth = = 0) {
intyear--; 0: One year reduction
Intmonth = 12;
Intday = 31;
}
else if (intmonth = = 4 | | intmonth = = 6 | | intmonth = = 9 | | intmonth = = 11) {
Intday = 30; 4,6,9,11:30 days
}
else if (intmonth = = 2) {
Intday = 28; 2:28/29
if (intyear% 4 = = 0) {
Intday = 29;
}
} else {
Intday = 31; 1,3,5,7,8,10,12:31 days
}
}

var strmonth = (Intmonth) < 10? "0" + (intmonth). ToString (): (Intmonth). ToString ();
var strday = (Intday) < 10? "0" + (intday). ToString (): (Intday). ToString ();
var strenddate = intyear + "-" + Strmonth + "-" + strday;
return strenddate;
}

Two. Related methods, function descriptions
var mydate = new Date ();
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get the full year (4-bit, 1970-????)
Mydate.getmonth (); Get the current month (0-11, 0 for January)
Mydate.getdate (); Get current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)
Mydate.gethours (); Get current number of hours (0-23)
Mydate.getminutes (); Gets the current number of minutes (0-59)
Mydate.getseconds (); Gets the current number of seconds (0-59)
Mydate.getmilliseconds (); Gets the current number of milliseconds (0-999), which is currently a 13-bit integer
Mydate.tolocaledatestring (); Get the current date, such as: June 17, 2016
Mydate.tolocaletimestring (); Gets the current time, such as: 11:41:24
Mydate.tolocalestring (); Gets the current datetime, for example: June 17, 2016 11:41:24

JS: Contract-known start date, age, automatic calculation of due date

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.