javascript之获取当前是第几周

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取当前是第几周</title>
<script type="text/javascript">
function getISOYearWeek(date){
        var commericalyear=getCommerialYear(date);
        var date2=getYearFirstWeekDate(commericalyear);   
        var day1=date.getDay();   
        if(day1==0) day1=7;   
        var day2=date2.getDay();   
        if(day2==0) day2=7;   
        var d = Math.round((date.getTime() - date2.getTime()+(day2-day1)*(24*60*60*1000)) / 86400000);     
        return Math.ceil(d / 7)+1; 
    }
    
    function getYearFirstWeekDate(commericalyear){
        var yearfirstdaydate=new Date(commericalyear, 9, 2);   
        var daynum=yearfirstdaydate.getDay(); 
        var monthday=yearfirstdaydate.getDate();
        if(daynum==0) daynum=7;
        if(daynum<=4){
            return new Date(yearfirstdaydate.getFullYear(),yearfirstdaydate.getMonth(),monthday+1-daynum);
        }else{
            return new Date(yearfirstdaydate.getFullYear(),yearfirstdaydate.getMonth(),monthday+8-daynum)
        } 
    }
    
    function getCommerialYear(date){
        var daynum=date.getDay(); 
        var monthday=date.getDate();
        if(daynum==0) daynum=7;
        var thisthurdaydate=new Date(date.getFullYear(),date.getMonth(),monthday+4-daynum);
        return thisthurdaydate.getFullYear();
    }
	function getResult(){
	
	    var tmp=new Date();
	    var y=tmp.getFullYear();
	    var m=tmp.getMonth()+1;
	    var d=tmp.getDate();
	    date=new Date(y,m,d);
	    var result=getISOYearWeek(date);
	    document.getElementById("result").innerHTML="现在是第"+result+"周";
	
	}

</script>
</head>

<body>
第一周开始日期:<input type="text" id="first_year"  value="2012"/><input type="text" name="first_month"   value="9" /><input type="text"  name="first_date"  value="2"/><br/>
<button  οnclick="getResult()">获取现在是第几周</button>
<span id="result" style=" color:red; width:150px; margin:0 auto; height:50px;"></span>
</body>
</html>

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页