Reprinted calendar objects ~~

Reprinted from the link: https://blog.csdn.net/nbin2008/article/details/79425494


/*
* Calendar Control
DW DateWeek var = new ()
    dt = new Date();
Dw.setDate(dt.getFullYear(),dt.getMonth()-0+1);
var list = Dw.getDayList (bool); // bool: true, adaptive length, the tail will not delete the first week of the month /. bool: false, fixed row 7 * 6 = 42 data
list = [
    {
        date:"2018-1-28"
        day:28
        siblings: true // last month or next month, date, month and used to distinguish between non-dates this month
        week: 0 // 0: Monday, 1: Tuesday. . .
    }
    ...
]
*/
(function(window){
    There 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) // determines whether a leap month
                mday[1] = 29;
            return mday[m - 1];
        },
        getWeek: function (y, m, d) {
            There wk;
            if (m <= 12 && m >= 1) {
                for (var i = 1; i < m; ++i) {
                    d += this.getDay(y, i);
                }
            }
            / * Calculate the date according to the formula weeks * /
            wk = (y - 1 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + d)% 7;
            // 0 corresponds to Sunday, 1 corresponds to Monday
            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){
            was tmp = date.match (/ \ d + / g);
            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),
                week load = this.getWeek (year, month, dayTotal);
            // last month's data
            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++;
            }
            // Month
            for (var i=1; i<=dayTotal; i++) {
                name = this.getName(year, month, i);
                this.setDay(name,i);
                index++;
            }
            // data next month
            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++;
            };
            // cache
            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) {
                was len = 7;
                    count = 0;
                for (var i = list.length -1; len> = 1; i -, len--) {
                    if (list[i]['siblings']) {
                        count++;
                    }
                }
                if (count==7) {
                    as = 7;
                    while (len) {
                        list.pop();
                        just--;
                    }
                }
            }
            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);

Instructions:

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

var list = Dw.getDayList(1); 

 

1. I wrote it myself when the coupling is very serious, and how to reduce the coupling between the DOM? Reprint articles to my help is for each data set as an object, such as the inside of the siblings, you can control whether the Month , if need other state, could be added.

2. Write your own code when the idea appears to be particularly limitations, such as the middle of the this.getDay () method, never thought to go getPrev () to take the opportunity to make up an object, but in fact can also be written using the same method, as long as inside the function judgment arguments.length can separate slightly, but how good it is in fact unclear, ooo, ooo T ^ T

3.var tmp = date.match (/ \ d + / gi); it can also be so used, ha ha.

Guess you like

Origin www.cnblogs.com/lblaoalblaoa/p/11373391.html