周视图日历计算方法

代码

function WeekLayoutCalendar() {
    //当前周
    var cells = document.getElementById('currWeek').getElementsByTagName('div');
    //周长度
    var clen = cells.length;
    console.log(clen);
    //当前周的第一天
    var currentFirstDate;
    //new两个当天
    var myDate = new Date();
    var etDate = new Date();
    //当前天的日期的日
    today = myDate.getDate();
    console.log(today);
    var formatDate = function (date) {
        var year = date.getFullYear() + '年';
        var month = (date.getMonth() + 1) + '月';
        var day = date.getDate();
        return day;
    };
    var addDate = function (date, n) {
        date.setDate(date.getDate() + n);
        return date;
    };
    var setDate = function (date) {
        var week = date.getDay();
        date = addDate(date, week * -1);
        currentFirstDate = new Date(date);
        var currM = currentFirstDate.getMonth();
        var todayM = myDate.getMonth();
        document.getElementById('currYM').innerHTML = currentFirstDate.getFullYear() + '-' + (1 + currentFirstDate.getMonth() < 10 ? '0' + parseInt(1 + currentFirstDate.getMonth()) : 1 + currentFirstDate.getMonth());
        myDate.setDate(myDate.getDate() - myDate.getDay());
        for (var i = 0; i < clen; i++) {
            cells[i].innerHTML = formatDate(i == 0 ? date : addDate(date, 1));
            if (cells[i].innerHTML == today && currentFirstDate.getMonth() == myDate.getMonth() && currentFirstDate.getFullYear() == myDate.getFullYear()) {
                cells[i].className = 'curr-day';
                document.getElementById('currYM').innerHTML = myDate.getFullYear() + '-' + (1 + etDate.getMonth() < 10 ? '0' + parseInt(1 + etDate.getMonth()) : 1 + etDate.getMonth());
            }
            else {
                cells[i].className = '';
            }
        }
    };

    document.getElementsByClassName('button-prev')[0].onclick = function () {
        setDate(addDate(currentFirstDate, -7));
    };
    document.getElementsByClassName('button-next')[0].onclick = function () {
        setDate(addDate(currentFirstDate, 7));
    };
    setDate(new Date());
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值