JS Date extension method

Source: Internet
Author: User

/*File Created: April*///date plus days to get new date//Datetemp need to participate in the calculation of the date, days to add the day, return the new date, date format: Yyyy-mm-ddfunctiongetnewday (Datetemp, days) {varDatetemp = Datetemp.split ("-"); varNdate =NewDate (datetemp[1] + '-' + datetemp[2] + '-' + datetemp[0]);//Convert to MM-DD-YYYY format    varMillseconds = Math.Abs (ndate) + (days * 24 * 60 * 60 * 1000); varRdate =NewDate (millseconds); varYear =rdate.getfullyear (); varmonth = Rdate.getmonth () + 1; if(Month <) month = "0" +month; varDate =rdate.getdate (); if(Date <) date = "0" +date; return(Year + "-" + month + "-" +date);}functiongetDate (strdate) {varDate = eval (' New Date ' (' + strdate.replace ') (/\d+ (? =-[^-]+$)/,             function(a) {returnparseint (A, 10)-1; }). Match (/\d+/g) + ') '); returndate;}//override the ToString method to convert the time to y-m-d h:i:s formatDate.prototype.toString =function () {    return  This. getFullYear () + "-" + ( This. GetMonth () + 1) + "-" + This. GetDate () + "" + This. GetHours () + ":" + This. getminutes () + ":" + This. getseconds ();}//format Time StringDate.prototype.toFormatString =function(format) {if(Format = = "") {        return  This. toString (); }    varstr = "; STR= Format.replace (/y|y/, This. getFullYear ()). Replace (/m|m/, This. GetMonth () + 1). Replace (/d|d/, This. GetDate ()). Replace (/h|h/, This. GetHours ()). Replace (/i|i/, This. getminutes ()). Replace (/s|s/, This. getseconds ()); returnstr;}//number of years to add at the current timeDate.prototype.addYear =function(years) {varCyear = This. getFullYear (); Cyear+=years;  This. Setyear (Cyear); return  This;}//add a number of days to the current timeDate.prototype.addDay =function(days) {varcd = This. GetDate (); CD+=Days ;  This. SetDate (CD); return  This;}//add a number of months at the current timeDate.prototype.addMonth =function(months) {varCM = This. GetMonth (); CM+=months;  This. setmonth (CM); return  This;}//Convert PHP time format (y-m-d h:i:s) to JS Date Objectfunctionphpdatetojsdate (phpdate) {if(Phpdate = = "") {        return NewDate (); }    return NewDate (Date.parse (Phpdate.replace (/-/g, "/")));}

JS Date extension method

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.