zoukankan      html  css  js  c++  java
  • 根据已知日期(yyyy-MM-dd)获取前n天的日期区间

    //获取天
    var  pubTime="2017-12-30"
     function buildDay(num){
                    num=num-1;
                    var myDate = new Date(pubTime); //获取今天日期
                    myDate.setDate(myDate.getDate() - num);
                    var dateArray = [];
                    var dateTemp;
                    var flag = 1;
                    for (var i = 0; i < num; i++) {
                        var month=myDate.getMonth()+1
                        if(month<10){
                            month="0"+month;
                        }
                        var day=myDate.getDate()
                        if(day<10){
                            day="0"+day;
                        }
                        dateTemp =myDate.getFullYear()+""+ month+""+day;
                        dateArray.push(dateTemp);
                        myDate.setDate(myDate.getDate() + flag);
                    }
                    dateArray.push(pubTime.replace(/-/g,""))
                    return dateArray
                }


    //获取月分
    function buildMonth(months){
        var now = new Date(pubTime);
    var cur_month = now.getMonth();
    now.setDate(1);//重置成1号,原因:在做减月份时,减去的月份没有31号,会自动加1到下个月份,导致计算失误

    if(months < 0){
    var _m = cur_month+months + 1;
    now.setMonth(_m);
    }

    var _months = [];
    for(var i = 0 ; i < Math.abs(months) ; i++){
    var year = now.getFullYear();
    var month = now.getMonth();
    if(i != 0 ){
    now.setMonth(month+1);
    }
    year = now.getFullYear();
    var _month = now.getMonth()+1;
    if(_month < 10) _month = '0'+_month;
    else _month = _month.toString();
    _months.push(year+_month);
    }
    //console.log('buildMonthData============>'+_months);
    return _months;
    },

      

  • 相关阅读:
    JQuery DOM操作
    JQuery 选择器和事件
    LinQ 组合查分页
    LinQ
    web 图片验证码 验证
    Web 上传图片加水印
    Web 组合查询加 分页
    ajax连接数据库加载+三级联动
    jq动画
    jq基础
  • 原文地址:https://www.cnblogs.com/liuhao-web/p/8340133.html
Copyright © 2011-2022 走看看