用javascript 取到当前月第一天、最后一天,本周第一天、最后一天

 

<script language=javascript> 
function showWeekFirstDay() 
{ 
var Nowdate=new Date(); 
var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); 
alert(WeekFirstDay) 
} 
function showWeekLastDay() 
{ 
var Nowdate=new Date(); 
var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); 
var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000); 
alert(WeekLastDay) 
} 
function showMonthFirstDay() 
{ 
var Nowdate=new Date(); 
var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1); 
alert(MonthFirstDay) 
} 
function showMonthLastDay() 
{ 
var Nowdate=new Date(); 
var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1); 
var MonthLastDay=new Date(MonthNextFirstDay-86400000); 
alert(MonthLastDay) 
} 
</script> 
<input name=haha type=button value="本周第一天" onclick=showWeekFirstDay()> 
<input name=haha type=button value="本周第七天" onclick=showWeekLastDay()> 
<input name=haha type=button value="本月第一天" onclick=showMonthFirstDay()> 
<input name=haha type=button value="本月最后一天" onclick=showMonthLastDay()>

 

 

 

选择一个日期计算出所在周的第一天(周日)和最后一天(周六)

 

页面

 页面代码

<table class="query_inner_table">
        	
			
			
			<tr>
			<td>开始时间</td>
			<td style="text-align:left"><input type="text" name="ngsj_Start"  value="2013-07-14"  style="border:none;"  readonly /></td>
			<td>结束时间</td>
			<td style="text-align:left"><input type="text" name="ngsj_End"  value="2013-07-20"   style="border:none;"  readonly /></td>
			</tr>
			
			<tr > 
				  <td align=center>选择日期</td>
		    	<td width="35%" style="text-align:left">
		          	<input name=nowDate type=text  value="2013-07-18" style="width:80px" readonly onpropertychange="javascript:calcDate();" onfocus="javascript:showCalendar(false,'yyyy-MM-dd');">
		          	<img src='/ahciq/resources/images/sys/cal.gif' onclick="javascript:document.forms[0].nowDate.focus();">
				</td>
				
			</tr>
		
			
</table>
		</td>
			 </tr>	
			<tr>
				<td align=center >
					<input type="button" class="btnsmall" value="确定" onclick="javascript:doCreate();">
					<input type="button" class="btnsmall" value="取消" onclick="window.location.href='WFreader.do?listID=409314302083702&processid=409310506143402'">
				</td>
			</tr>
		</table>

 js代码

<script language='javascript'>
function doCreate(){

	//判断输入的日期是否是下个星期
	var nowDate=new Date();
  	var nowStr=UTCConvertDate2(getLastDateOfWeek(nowDate)); //本周的最后一天
  	var d1=new Date(nowDate);
  	//用户输入时间
  	var inputDate=document.forms[0].nowDate.value;
  	var d2=new Date(inputDate.replace(/-/g,"/"));
  	
  	if(Date.parse(d1)<Date.parse(d2)){
  		alert("输入日期不能在本周以后!");
  		document.forms[0].nowDate.focus();
  		return false;
  	}else{
		document.forms[0].sType.value= "underway";
		document.forms[0].wfreadonly.value='n';
		document.forms[0].processid.value= '409310506143402' ;				
		document.forms[0].act.value = "open";
		document.forms[0].action = "WFreader.do";
		document.forms[0].target = "_self";
		document.forms[0].submit();
	}
}


function calcDate(){
	
    var inputDate=document.forms[0].nowDate.value;
    if(''==inputDate){
    	document.forms[0].ngsj_Start.value='';
    	document.forms[0].ngsj_End.value='';
    }else{
    	var theDate=new Date(Date.parse(inputDate.replace(/-/g,"/"))); 
	    //开始时间
	    var d1=getFirstDateOfWeek(theDate);
	  	document.forms[0].ngsj_Start.value=UTCConvertDate(d1);
	  	
	  	//结束时间
	 	 var d2=getLastDateOfWeek(theDate);
	  	document.forms[0].ngsj_End.value=UTCConvertDate(d2);
    }
}

function UTCConvertDate(utc) { 
  var d=utc .getFullYear();
  if(utc .getMonth()+1<10){
     d+= '-0'+(utc .getMonth()+1);
  }else{
  	  d+='-'+(utc .getMonth()+1);
  }
  if(utc.getDate()<10){
     d+='-0'+utc.getDate();
  }else{
  	 d+='-'+utc.getDate();
  }
  return  d;
   
} 


//得到每周的第一天(周日)
function getFirstDateOfWeek(theDate){
	var firstDateOfWeek;
	theDate.setDate(theDate.getDate() - theDate.getDay()); //	 
	firstDateOfWeek = theDate;
	return firstDateOfWeek;	
}
//得到每周的最后一天(周六)
function getLastDateOfWeek(theDate){
	var lastDateOfWeek;
	theDate.setDate(theDate.getDate() + 6 - theDate.getDay()); //	 
	lastDateOfWeek = theDate;
	return lastDateOfWeek;	
}

//格式yyyy-MM-dd日期
function UTCConvertDate2(utc) { 
  return  utc .getFullYear()+ "/" + (utc .getMonth()+1) + "/" + utc.getDate();
} 
</script>

 

你可能感兴趣的:(JavaScript)