转载的日历对象~~

a 夏天 提交于 2019-11-27 19:01:54

转载自链接:https://blog.csdn.net/nbin2008/article/details/79425494


/*
*   日历控件
var Dw = new DateWeek(),
    dt = new Date();
Dw.setDate(dt.getFullYear(),dt.getMonth()-0+1);
var list = Dw.getDayList(bool); //bool:true,自适应长度,会删除首/尾不是当月的一周。bool:false,固定7行*6列=42条数据
list = [
    {
        date:"2018-1-28"
        day:28
        siblings:true   //上月或者下月的日期,用于区分本月和非本月的日期
        week:0  //0:星期一,1:星期二。。。
    }
    ...
]
*/
(function(window){
    var proto = {
        getDay: function(y, m) {
            var mday = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
            if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) //判断是否是闰月
                mday[1] = 29;
            return mday[m - 1];
        },
        getWeek: function(y, m, d) {
            var wk;
            if (m <= 12 && m >= 1) {
                for (var i = 1; i < m; ++i) {
                    d += this.getDay(y, i);
                }
            }
            /*根据日期计算星期的公式*/
            wk = (y - 1 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + d) % 7;
            //0对应星期天,1对应星期一
            return parseInt(wk);
        },
        getName: function(year,month,day){
            return year + "-" + month + "-" + day;
        },
        getPrev: function(y,m){
            if( m-1 == 0){
                return {
                    y: y-1,
                    m: 12,
                    d: this.getDay(y-1, 12)
                };
            }else{
                return {
                    y: y,
                    m: m-1,
                    d: this.getDay(y, m-1)
                };
            }
        },
        getNext: function(y,m){
            if( m+1 > 12 ){
                return {
                    y: y+1,
                    m: 1,
                    d: this.getDay(y+1, 1)
                };
            }else{
                return {
                    y: y,
                    m: m+1,
                    d: this.getDay(y, m+1)
                };
            };
        },
        setDay: function(date,day,siblings){
            var tmp = date.match(/\d+/gi);
            this.dayList.push({
                date: date,
                day: day,
                week: this.getWeek(+tmp[0],+tmp[1],+tmp[2]),
                siblings: !!siblings,
            })
        },
        clear: function(){
            this.dayList = [];
        },
        setDate: function(year, month){
            var cache_name = year + "-" + month;
            if (this.cache[cache_name]) {
                this.dayList = this.cache[cache_name];
                return this;
            }
            //
            this.clear();
            var name = null,
                index = 0,
                year = parseInt(year),
                month = parseInt(month),
                dayTotal = this.getDay(year, month),
                weekFirst = this.getWeek(year,month,1),
                weekLast = this.getWeek(year,month,dayTotal);
            //上月的数据
            var prev = this.getPrev(year, month),
                prevDate = prev.d - weekFirst + 1;
            for (var i=0; i<weekFirst; i++) {
                name = this.getName(prev.y, prev.m, prevDate);
                this.setDay(name,prevDate,1);
                prevDate++;
                index++;
            }
            //本月数据
            for (var i=1; i<=dayTotal; i++) {
                name = this.getName(year, month, i);
                this.setDay(name,i);
                index++;
            }
            //下月数据
            var next = this.getNext(year, month),
                day = 1;
            while (index<this.maxLen) {
                name = this.getName(next.y, next.m, day);
                this.setDay(name,day,1);
                index++;
                day++;
            };
            //缓存
            this.cache[cache_name] = JSON.parse(JSON.stringify(this.dayList));
            return this;
        },
        getDayList: function(bool) {
            var list = JSON.parse(JSON.stringify(this.dayList));
            if (bool) {
                var len = 7;
                    count = 0;
                for (var i=list.length-1; len>=1; i--,len--) {
                    if (list[i]['siblings']) {
                        count++;
                    }
                }
                if (count==7) {
                    len = 7;
                    while (len) {
                        list.pop();
                        len--;
                    }
                }
            }
            return list;
        },
        init: function(){
            this.cache = {};
            this.dayList = [];
            this.maxLen = 42;
            return this;
        }
    }
    function DateWeek(){
        return this.init();
    };
    DateWeek.prototype = proto;
    DateWeek.prototype.constructor = DateWeek;
    window.DateWeek = DateWeek;
})(window);

使用方法:

var Dw = new DateWeek(),
    dt = new Date();
Dw.setDate(dt.getFullYear(), +dt.getMonth()+1);

var list = Dw.getDayList(1); 

 

1.本人自己写的时候耦合非常严重,要如何减少和DOM之间的耦合?转载的文章给我的帮助是每项数据都设置成对象,如里面的siblings,就可以控制是否为本月数据,如果还需要别的状态,还可以再加.

2.自己写代码的时候思想好像特别局限,如中间的this.getDay()方法,都没想过走getPrev()去借机凑成一个对象,不过其实也可以用同一个方法写入,只要在函数里面判断arguments.length就可以分离咯,但是怎么样才是好的呢其实也不清楚,呜呜呜T^T

3.var tmp  = date.match(/\d+/gi);还可以这么用,哈哈哈哈哈.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!