JS Date plugin datehelp Get this month, three months, this year's date _javascript tips

Source: Internet
Author: User

Recently read some about object-oriented knowledge, the most recent work in doing statistical queries need to use this month, nearly three months, this year's date range, so the following with object-oriented thinking to write a 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 ',//, starting from date:
' Yyyy/mm/dd '
});

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

Datehelp.js Plugin

/** * Through calls can be obtained this month, nearly three months, this year's date * @param obj * @constructor/function datehelp (obj) {/*var obj = {date: ' 2015-02-01 ',//from this date the type: ' Month ',//is calculated forward by month and day: Years (year), months (month), Days (day) value: ' 14 ',//forward calculated values, month and day format: ' YYYY/MM/DD '//date format}*
 /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 the year/day if (this.date instanceof date) {///processing the DATE function of the This.year = This.date.getFullYear ();
  This.month = This.date.getMonth () +1;
 This.day = This.date.getDate ();
  }else{//handling the incoming this.year = this.date.substr (0, 4) of the non-date function;
  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.val

 UE, _year = this.year, _month = this.month, _day = This.day;
 if (_type = = ' Year ' | | | _type = = ' Years ') {_year = _value; }else if (_type = = ' Month ' | |
  _type = = ' month ') {_year-= parseint (_VALUE/12);
  _month-= _value% 12;
   if (_month <= 0) {_year = 1;
  _month + 12; }else if (_type = = ' Day ' | | | | | _type = = ' Days ') {}else {} var date = new Date (_year, _month-1, _day) return THIS.FO
Rmatdate (date, this.format); } DateHelp.prototype.formatDate = function (date,fmt) {var o = {"m+": Date.getmonth () +1,//month "d+": date.getd
  Ate (),//day "h+": date.gethours (),//h "m+": date.getminutes (),//min "s+": Date.getseconds (),//sec
 "q+": Math.floor (Date.getmonth () +3)/3),//Quarter "S": date.getmilliseconds ()//millisecond};
 if (/(y+)/.test (FMT)) Fmt=fmt.replace (Regexp.$1, (date.getfullyear () + ""). substr (4-regexp.$1.length)); For (var k in O) if (new RegExp ("+ K +")). Test (FMT)) FMT = Fmt.replace (regexp.$1, regexp.$1.length==1)?
 (O[k]): (("+ o[k]"). substr (("" + o[k). length));
return FMT; } DateHelp.prototype.getThisMonth = function () {var-i = new Date (This.year,THIS.MONTH-1);

 var last = new Date (this.year, this.month, 0);

Return This.formatdate (A/This.format) + "-" + this.formatdate (last, This.format);
DateHelp.prototype.getThreeMonth = function () {return this.beforedate (' month ', 3) + "-" + this.beforedate (' Day ', 0);
 } DateHelp.prototype.getThisYear = function () {var = new Date (this.year, 0, 1);

 var last = new Date (this.year, 11, 31);


Return This.formatdate (A/This.format) + "-" + this.formatdate (last, This.format);

/*//example var mydate = new Datehelp ({date: ' 2015-02-01 ',////) to start Computing format: ' YYYY/MM/DD '});
Console.log (Mydate.getthismonth ());
Console.log (Mydate.getthreemonth ());
 Console.log (Mydate.getthisyear ());

HTML Test Code

<! DOCTYPE html>
 
 

I hope this article will help you learn about JavaScript programming.

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.