The js date plug in gets the dates for this month three months and this year

  • 2021-01-22 04:51:27
  • OfStack

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

This is done directly through new and DateHelp


var myDate = new DateHelp({
date:'2015-02-01',// Count from this date 
format:'yyyy/MM/dd'
});

myDate.getThisMonth();
myDate.getThreeMonth();
myDate.getThisYear();

dateHelp js plug-ins


/**
 *  By calling can get this month, near 3 Months, the date of the year 
 * @param obj
 * @constructor
 */
function DateHelp(obj) {
 /*var obj = {
  date:'2015-02-01',// Count from this date 
  type:'month',// Forward in year/month/day: year ( year ), month ( month ), day ( day ) 
  value:'14',// Numbers computed forward, year, month, day 
  format:'yyyy/mm/dd'// The 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 year, month and day 
 if (this.date instanceof Date){
  // Handle the incoming date function 

  this.year = this.date.getFullYear();
  this.month = this.date.getMonth()+1;
  this.day = this.date.getDate();
 }else{
  // Handles incoming undate functions 

  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 == ' month '){
  _year -= parseInt(_value / 12);
  _month -= _value % 12;
  if(_month <= 0){
   _year -= 1;
   _month += 12;
  }
 }else if (_type == 'day' || _type == ' day '){

 }else {

 }

 var date = new Date(_year, _month - 1, _day)
 return this.formatDate(date, this.format);
}

DateHelp.prototype.formatDate = function(date,fmt){

 var o = {
  "M+" : date.getMonth()+1,     // in 
  "d+" : date.getDate(),     // day 
  "h+" : date.getHours(),     // hours 
  "m+" : date.getMinutes(),     // points 
  "s+" : date.getSeconds(),     // seconds 
  "q+" : Math.floor((date.getMonth()+3)/3), // quarter 
  "S" : date.getMilliseconds()    // ms 
 };
 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]) : (("00"+ o[k]).substr((""+ o[k]).length)));
 return fmt;
}

DateHelp.prototype.getThisMonth = function() {

 var first = new Date(this.year, this.month - 1);
 var last = new Date(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() {

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

 return this.formatDate(first, this.format) + " - " + this.formatDate(last, this.format);
}


/*
// The sample 
var myDate = new DateHelp({
 date:'2015-02-01',// Count from this date 
 format:'yyyy/MM/dd'
});

console.log(myDate.getThisMonth());
console.log(myDate.getThreeMonth());
console.log(myDate.getThisYear());*/

html tests the code


<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <title></title>
 <script src="myJs/dateHelp.js"></script>
</head>
<body>


<script>

 var myDate = new DateHelp({
  date:new Date(),// Count from this date 
  format:'yyyy/MM/dd'
 });

 console.log(myDate.getThisMonth());
 console.log(myDate.getThreeMonth());
 console.log(myDate.getThisYear());
</script>
</body>
</html>

I hope this article is helpful for you to learn javascript programming.


Related articles: