JS Date Operation class

Source: Internet
Author: User

/* Date Object formatted as specified date format */
Date.prototype.format = function (format) {
var o = {
"m+": This.getmonth () + 1,//month
"D+": This.getdate (),//day
"H +": this.gethours (),//hour
"m+": This.getminutes (),//minute
"S+": This.getseconds (),//second
"q+": Math.floor ((This.getmonth () + 3)/3),//quarter
"S": This.getmilliseconds ()//millisecond
}
if (/(y+)/.test (format)) format = Format.replace (regexp.$1,
(This.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O) if (New RegExp ("(" + K + ")"). Test (format))
Format = Format.replace (regexp.$1,
Regexp.$1.length = = 1? O[K]:
("XX" + o[k]). substr (("" + o[k]). length);
return format;
}
/*
* Parameters
*dateobj
* Required option. Any Date object.
*interval
* Required option. A string expression that represents the time interval to add. See the Settings section for values.
*number
* Required option. A numeric expression that represents the number of time intervals to add. A numeric expression can be a positive number (get a future date) or negative (get a date in the past).
The *interval parameter can have the following values:
* Setting description
*y year
*q Quarter
*m Month
*d Day
*w Week
*h hours
*n min
* Date of date, month, day, etc plus number
*/
DATE.PROTOTYPE.DATEADD = function (interval, number) {
var d = this;
var k = {' Y ': ' fullyear ', ' Q ': ' Month ', ' m ': ' Month ', ' W ': ' Date ', ' d ': ' Date ', ' H ': ' Hours ', ' n ': ' Minutes ', ' s ': ' Secon DS ', ' ms ': ' MilliSeconds '};
var n = {' Q ': 3, ' W ': 7};
Eval (' D.set ' + k[interval] + ' (d.get ' + k[interval] + ' () + ' + ((N[interval] | | 1) * number) + ');
return D;
}

/* Calculates dates that differ by two dates, month and day, etc. */
Date.prototype.dateDiff = function (interval, objDate2) {
var d = This, I = {}, T = D.getti Me (), t2 = Objdate2.gettime ();
i[' y '] = objdate2.getfullyear ()-d.getfullyear ();
i[' q '] = i[' y '] * 4 + math.floor (Objdate2.getmonth ()/4)-Math.floor (D.getmonth ()/4);
i[' m '] = i[' y '] * + objdate2.getmonth ()-D.getmonth ();
i[' ms '] = objdate2.gettime ()-d.gettime ();
i[' W '] = Math.floor ((t2 + 345600000)/(604800000))-Math.floor ((t + 345600000)/(604800000));
i[' d '] = Math.floor (t2/86400000)-Math.floor (t/86400000);
i[' h '] = Math.floor (t2/3600000)-Math.floor (t/3600000);
i[' n '] = Math.floor (t2/60000)-Math.floor (t/60000);
i[' s '] = Math.floor (t2/1000)-Math.floor (t/1000);
return I[interval];
}
/* Date string converted to date Object */
Function parsedate (str) {

if (typeof str = = ' string ') {
var results = Str.match (/^ * (\d{4})-(\d{1,2})-(\d{1,2}) *$/);
if (Results && results.length > 3)
return new Date (number (results[1]), number (results[2])-1, Number (results[3]));
Results = Str.match (/^ * (\d{4})-(\d{1,2})-(\d{1,2}) + (\d{1,2}):(\d{1,2}):(\d{1,2});
if (Results && results.length > 6)
return new Date (number (results[1]), number (results[2])-1, Number (results[3]), number (results[4]), number (results[5]) , number (results[6]));
Results = Str.match (/^ * (\d{4})-(\d{1,2})-(\d{1,2}) + (\d{1,2}):(\d{1,2}):(\d{1,2}) \. (\d{1,9}) *$/);
if (Results && results.length > 7)
return new Date (number (results[1]), number (results[2])-1, Number (results[3]), number (results[4]), number (results[5]) , number (results[6]), number (results[7]));

}
return null;
}

function Rain () {
Obj1 = new Date (). Format ("Yyyy-mm-dd hh:mm:ss");
Obj2 = new Date (). Format ("Yyyy-mm-dd");
Obj3 = new Date (). Format ("Yyyy/mm/dd");
Obj4 = new Date (). Format ("mm/dd/yyyy");
Obj5 = new Date (). Format ("Yyyy-mm-dd hh:mm");
Alert ("obj1=" + obj1 + "/n obj2=" + obj2 + "/n obj3=" + obj3 + "/n obj4=" + obj4);
alert (OBJ5);
var str = "2010-1-10 22:10:10";
var dates = parsedate (str);
Alert (Dates.getdate ());
OBJ7 = Dates.format ("Yyyy-mm-dd");
alert (OBJ7);
//}
Rain ();

JS Date Operation class

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.