栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

js日期插件dateHelp获取本月、三个月、今年的日期

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

js日期插件dateHelp获取本月、三个月、今年的日期

最近看了一些关于面向对象的知识,最近工作中在做统计查询的时候需要用到本月、近三个月、今年的日期范围,所以下面用用面向对象的思想写了一个获取日期的插件,大家可以借鉴使用。

直接通过new DateHelp就可以调用了

var myDate = new DateHelp({
date:'2015-02-01',//从此日期开始计算
format:'yyyy/MM/dd'
});

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

dateHelp.js插件


function DateHelp(obj) {
 

 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';

 //日期和非日期格式获取年月日
 if (this.date instanceof Date){
  //处理传进来的是日期函数的

  this.year = this.date.getFullYear();
  this.month = this.date.getMonth()+1;
  this.day = this.date.getDate();
 }else{
  //处理传入的是非日期函数的

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

 }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,     //月份
  "d+" : date.getDate(),     //日
  "h+" : date.getHours(),     //小时
  "m+" : date.getMinutes(),     //分
  "s+" : date.getSeconds(),     //秒
  "q+" : Math.floor((date.getMonth()+3)/3), //季度
  "S" : date.getMilliseconds()    //毫秒
 };
 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);
}



html测试代码




 
 
 







希望本文所述对大家学习javascript程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/98063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号