js获取 当前年 有 几周 每一周是从几号到几号

灰原哀和柯南 2015-09-11 10:57:38
例如:
2015年第01周 01月01号-01月07号
2015年第02周 01月08号-01月15号
...全文
494 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
forrg 2015-09-14
  • 打赏
  • 举报
回复
其实不用写这么多,以前整个项目的时候用到过,里面有相应的函数的,楼主搜索看看
似梦飞花 2015-09-12
  • 打赏
  • 举报
回复 1

var Week=(function(){
        var ONE_DAY=24*3600*1000,
            ONE_WEEK=7*ONE_DAY;
        function formatNumber(num){
            return (num>9?'':'0')+num;
        }
        function formatDate(date,num){
            var year=date.getFullYear(),
                month=formatNumber(date.getMonth()+1),
                day=formatNumber(date.getDate()),
                nextWeek=new Date(+date+ONE_WEEK),
                nextWeekYear=nextWeek.getFullYear(),
                nextWeekMonth=formatNumber(nextWeek.getMonth()+1),
                nextWeekday=formatNumber(nextWeek.getDate());
            return year+'年第'+formatNumber(num+1)+'周'+month+'月'+day+'号-'+nextWeekYear+'年'+nextWeekMonth
                    +'月'+nextWeekday+'号';
        }
        function Week(year){
            this.year=new Date(year,0,1);
            this.nextYear=new Date(year+1,0,1);
            this.days=0;
            this.weeks=0;
        }
        Week.prototype.getDays=function(){
            return this.days=Math.ceil((this.nextYear-this.year)/ONE_DAY);
        }
        Week.prototype.getWeeks=function(){
            return this.weeks=Math.ceil(this.days||this.getDays()/7);
        }
        Week.prototype.getSomeWeek=function(num){
            return formatDate(new Date(+this.year+ONE_WEEK*num),num);
        }
        Week.prototype.showWeek=function(num){
            num=parseInt(num-1)||0;
            if(!this.weeks){
                this.getWeeks();
            }
            if(num>this.weeks){
                num=0;
            }
            if(num){
                return this.getSomeWeek(num);
            }else{
                var arr=[];
                while(num<this.weeks){
                    arr.push(this.getSomeWeek(num));
                    num++;
                }
                return arr;
            }
        }
        return Week;
    })();
    var d=new Week(2000);
    console.log(d.showWeek(),null,4);

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧