码迷,mamicode.com
首页 > 其他好文 > 详细

获取选择的当前天、周、月、年的时间段

时间:2017-05-06 15:13:15      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:create   int   format   created   out   格式   return   form   lda   

/**
 * Created by Administrator on 2017/5/6.
 */
/**
 * options:{"type":2,"date":"2017-5-6","connect":"-"}
 * type:"1" 获取当天
 * type:"2" 获取本周
 * type:"3" 获取本月
 * type:"4" 获取本年
 */
function GetDatePeriod(options) {
    this.options={
        date:new Date(),
        type:1,
        connect:"-"
    };
    for(var key in options){
        if(options[key]!=""){
            this.options[key]=options[key];
        }
    }
    var selD=new Date(this.options.date);
    var selDayOfWeek=selD.getDay();
    var selDate=selD.getDate();
    var selMonth=selD.getMonth();
    var selYear=selD.getFullYear();
    var startDate, endDate;
    //补齐数位
    this.digit = function(num) {
        return num < 10 ? "0" + (num | 0) :num;
    };
    //判断闰年
    this.isLeap = function(y) {
        return (y % 100 !== 0 && y % 4 === 0) || (y % 400 === 0);
    };
    //获取本月天数
    this.getDaysNum = function(y, m) {
        var num = 31;
        switch (parseInt(m)) {
            case 2:
                num = this.isLeap(y) ? 29 : 28; break;
            case 4: case 6: case 9: case 11:
            num = 30; break;
        }
        return num;
    };
    //日期格式化
    this.dateFormat=function(dateSrc){
        var connect=this.options.connect;
        return dateSrc.getFullYear()+connect+this.digit(dateSrc.getMonth()+1)+connect+this.digit(dateSrc.getDate())
    };
    //1:本日,2:本周,3:本月,4:本年
    switch (this.options.type) {
        case "1":
            startDate = new Date(selYear, selMonth, selDate);
            endDate = new Date(selYear, selMonth, selDate);
            break;
        case "2":
            if (selDayOfWeek > 0) {
                startDate = new Date(selYear, selMonth, selDate - selDayOfWeek + 1);
                endDate = new Date(selYear, selMonth, selDate + (7 - selDayOfWeek));
            } else {
                startDate = new Date(selYear, selMonth, selDate - selDayOfWeek - 6);
                endDate = new Date(selYear, selMonth, selDate);
            }
            break;
        case "3":
            startDate = new Date(selYear, selMonth, 1);
            endDate = new Date(selYear, selMonth,this.getDaysNum(selYear,selMonth+1));
            break;
        case "4":
            startDate = new Date(selYear, 0, 1);
            endDate = new Date(selYear, 11, 31);
            break;
    }
    this.periodOutput=function(){
        //返回本周,月,年的起止时间
        startDate=this.dateFormat(startDate);endDate=this.dateFormat(endDate);
        //如果选择的时间大于当前时间则把endDate设置为当前时间
        var curDate=this.dateFormat(new Date());
        endDate=new Date(endDate).getTime()>new Date(curDate).getTime()?curDate:endDate;
        return {
            startDate:startDate,
            endDate:endDate
        }
    }
};
console.info("当前周时间段",new GetDatePeriod({type:"2",date:"2017-05-06",‘connect‘:‘/‘}).periodOutput());

  

获取选择的当前天、周、月、年的时间段

标签:create   int   format   created   out   格式   return   form   lda   

原文地址:http://www.cnblogs.com/leyi/p/6816654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!