JS get this month, three months, this year's date plugin Datehelp

Source: Internet
Author: User

Recently read some about object-oriented knowledge, in the recent work in the statistical query need to use this month, nearly three months, this year's date range, so the following with the idea of object-oriented to write a get date plug-in, we can use for reference.

Directly through the new DATEHELP, you can call the

var mydate = new Datehelp ({
Date: ' 2015-02-01 ',//start calculation from this date
Format: ' Yyyy/mm/dd '
});

Mydate.getthismonth ();
Mydate.getthreemonth ();
Mydate.getthisyear ();

Datehelp.js Plug-in
/** * By calling can get this month, nearly three months, this year's date * @param obj * @constructor*/functiondatehelp (obj) {/*var obj = {date: ' 2015-02-01 ',//The type is calculated from this date: ' Month ',//month-by-year: years, months, days (day) value: ' 14 ',//forward calculation The numeric value, month and day format: ' YYYY/MM/DD '//date formats}*/     This. Date =obj.date;  This. Type =Obj.type;  This. Value = Obj.value = = undefined? obj.value:0;  This. Format = Obj.format = = undefined? Obj.format: ' Yyyy/mm/dd '; //Date and non-date formats get month day    if( This. DateinstanceofDate) {        //processing passes in a date function.         This. Year = This. Date.getfullyear ();  This. Month = This. Date.getmonth () +1;  This. Day = This. Date.getdate (); }Else{        //That handles the incoming non-date function.         This. Year = This. DATE.SUBSTR (0, 4);  This. Month = This. DATE.SUBSTR (5, 2);  This. Day = This. DATE.SUBSTR (8, 2); }}datehelp.prototype.beforedate=function(type, value) {var_type = Type | | This. Type, _value= value | | This. Value, _year= This. Year, _month= This. Month, _day= This. Day; if(_type = = ' Year ' | | _type = = ' Years ') {_year-=_value; }Else if(_type = = ' Month ' | | _type = = ' Moon ') {_year-= parseint (_VALUE/12); _month-= _value% 12; if(_month <= 0) {_year-= 1; _month+ = 12; }    }Else if(_type = = ' Day ' | | _type = = ' Days '){    }Else {    }    varDate =NewDate (_year, _month-1, _day)return  This. FormatDate (Date, This. format);} DateHelp.prototype.formatDate=function(date,fmt) {varn \ {        "m+": Date.getmonth () +1,//Month"D+": date.getdate (),//Day"H +": date.gethours (),//hours"m+": date.getminutes (),//points"S+": Date.getseconds (),//seconds"q+": Math.floor ((Date.getmonth () +3)/3),//Quarterly "S": Date.getmilliseconds ()//milliseconds    }; if(/(y+)/. Test (FMT) FMT=fmt.replace (Regexp.$1, (date.getfullyear () + ""). substr (4-regexp.$1. length));  for(varKincho)if(NewRegExp ("(" + K + ")"). Test (FMT) FMT= Fmt.replace (regexp.$1, (regexp.$1.length==1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); returnFMT;} DateHelp.prototype.getThisMonth=function() {    varFirst =NewDate ( This. Year, This. month-1); varLast =NewDate ( This. Year, This. Month, 0); return  This. FormatDate (First, This. Format) + "-" + This. FormatDate (Last, This. format);} DateHelp.prototype.getThreeMonth=function() {    return  This. Beforedate (' month ', 3) + "-" + This. Beforedate (' Day ', 0);} DateHelp.prototype.getThisYear=function() {    varFirst =NewDate ( This. year, 0, 1); varLast =NewDate ( This. year, 11, 31); return  This. FormatDate (First, This. Format) + "-" + This. FormatDate (Last, This. format);}/*//Example var mydate = new Datehelp ({date: ' 2015-02-01 ',//calculated from this date format: ' Yyyy/mm/dd '}); Console.log (Mydate.getthismon th ()); Console.log (Mydate.getthreemonth ()); Console.log (Mydate.getthisyear () );*/

HTML Test Code
<!DOCTYPE HTML><HTML><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title></title>    <Scriptsrc= "Myjs/datehelp.js"></Script></Head><Body><Script>    varmydate= Newdatehelp ({date:NewDate (),//start calculation from this dateformat:'YYYY/MM/DD'    });    Console.log (Mydate.getthismonth ());    Console.log (Mydate.getthreemonth ()); Console.log (Mydate.getthisyear ());</Script></Body></HTML>

JS get this month, three months, this year's date plugin Datehelp

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.