js根据年份计算总周数并获取每周的日期范围

场景是这样,选择年份获取每周日期范围

html

<div class="btn-group">
    <button type="button" class="my my1 btn btn-primary dropdown-toggle ft18" data-toggle="dropdown" style="width: 190px;">
        <span><img src="img/icon-clock.png" width="22"></span>&nbsp;&nbsp;
        <span class="yearText priceData">选择年份</span>&nbsp;&nbsp;
        <span class="caret"></span>
    </button>
    <ul class="my line20 dropdown-menu ft16" role="menu" style="width: 190px;" id="priceDataYear">
    </ul>
</div>

 显示近8年的年份

/**
 * 显示近8年的年份
 *
 */
function showYear() {
    var nowDate = new Date();
    //设置近8年的年份
    var nowYear = nowDate.getFullYear();
    var yearHtml = '<li><a href="javaScript:void(0)" onclick="year($(this).text())">选择年份</a></li>';
    for (var i = 0; i < 8; i++) {
        yearHtml += '<li><a href="javaScript:void(0)" onclick="year($(this).text())">' + (nowYear - i) + '年</a></li>';
    }
    $('#priceDataYear').html(yearHtml);
}

获取一年的总周数,循环周数并获取每周的日期范围 

/**
 * 点击年份,显示选中的年份,传入选中的年份显示周数和日期范围
 * @param text
 */
function year(text) {
    $('.yearText.priceData').text(text);
    $('.weekText').text('选择周数');
    var weekHtml = '';
    if (a != '选择年份') {
        var year = parseInt(a.substring(0, a.length - 1));
        //计算出这年的周数
        var weekNum = getNumOfWeeks(year);
        //首先算出这年的第一个星期日
        var firstSunday = new Date(year, 0, 1);
        var n = 6 - (firstSunday.getDay() + 6) % 7;
        firstSunday.setDate(firstSunday.getDate() + n);
        //根据年份设置周数
        weekHtml += '<li><a href="javaScript:void(0)" onclick="week($(this).text())">选择周数</a></li>';
        for (var i = 1; i <= weekNum; i++) {
            if (i == 1) {
                //计算这年第一个周一的日期
                var firstMonday = new Date(firstSunday.setDate(firstSunday.getDate() - 6));
                firstSunday.setDate(firstSunday.getDate() + 6);
                weekHtml += '<li><a href="javaScript:void(0)" onclick="week($(this).text())">第' + i + '周(' + getNowFormatDate(firstMonday) + ')-(' + getNowFormatDate(firstSunday) + ')</a></li>';
            } else {
                weekHtml += '<li><a href="javaScript:void(0)" onclick="week($(this).text())">第' + i + '周' + getDateRange(firstSunday) + '</a></li>';
                //计算出下一个星期日,有个问题是上面调用getDateRange()已经firstSunday加了7天,这里就不需要重新firstSunday加7天
                // firstSunday.setDate(firstSunday.getDate() + 7);
            }
        }
    }
    $('#priceDataWeek').html(weekHtml);
}

 计算一年的总周数

/**
 * 根据年份计算总周数
 * @param year
 * @returns {number}
 */
function getNumOfWeeks(year) {
    //设置为这一年开始日期
    var startDateOfYear = new Date(year, 0, 1);
    //计算这一年有多少天
    var daysOfYear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365;
    //366(365)/7=52.2(52.1),所以一般一年有52周余1天或者2天,当这一年有366天时且第一天是周日,那么他的最后一天则是周一,这一年就有54周。
    var weekNum = 53;
    //当年份是闰年且第一天是周日时有54周
    if (startDateOfYear.getDay() == 0 && daysOfYear == 366) {
        weekNum = 54;
    }
    return weekNum;
}

 根据传入的上周日日期获取本周的日期范围并转换成所需的日期格式

/**
 * 根据上周日获取这周日的日期范围
 * @param lastSunday
 * @returns {string}
 */
function getDateRange(lastSunday) {
    if (lastSunday == null || lastSunday == '') {
        return "";
    }
    var beginDate = new Date(lastSunday.setDate(lastSunday.getDate() + 1));
    var endDate = new Date(lastSunday.setDate(lastSunday.getDate() + 6));
    return '(' + getNowFormatDate(beginDate) + ')-' + '(' + getNowFormatDate(endDate) + ')';
}

/**
 * 时间转换成字符串
 * @param date
 * @returns {string}
 */
function getNowFormatDate(date) {
    var Month = 0;
    var Day = 0;
    var CurrentStr = "";
    // 初始化时间
    Month = date.getMonth() + 1;
    Day = date.getDate();
    if (Month >= 10) {
        CurrentStr += Month + "月";
    } else {
        CurrentStr += "0" + Month + "月";
    }
    if (Day >= 10) {
        CurrentStr += Day + "日";
    } else {
        CurrentStr += "0" + Day + "日";
    }
    return CurrentStr;
}

最后的效果:

 

 

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值