`
悠悠晃晃
  • 浏览: 39980 次
  • 性别: Icon_minigender_1
  • 来自: 山东
社区版块
存档分类
最新评论

JS计算某天后几天的日期/某天几个周后的周几日期

阅读更多
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=gb2312">
<title>获取本周、上一周和下一周周一至周日时间</title>
</head>
<script language="javascript">
function setWeekTime(weekTime){
	var time=new Array(); 
	if(weekTime=='last'){ 
		time=getLastWeekDate();
	}
	if(weekTime=='this'){ 
		time=getThisWeekDate();
	}
	if(weekTime=='next'){ 
		time=getNextWeekDate();
	}
	document.getElementById("monday").value=time[0];
	document.getElementById("tuesday").value=time[1];
	document.getElementById("wednesday").value=time[2];
	document.getElementById("thursday").value=time[3];
	document.getElementById("friday").value=time[4];
	document.getElementById("saturday").value=time[5];
	document.getElementById("sunday").value=time[6];
} 
//获得本周的周一和周末
function getThisWeekDate(){
	var now = new Date();
	var week=new Array();
	var currentWeek = now.getDay();
	if ( currentWeek == 0 ){
		currentWeek = 7;
	}
 
	var monday = now.getTime() -(currentWeek-1)*24*60*60*1000;  //星期一
	var tuesday  =now.getTime() - (currentWeek-2)*24*60*60*1000; //星期二
	var wednesday = now.getTime() -(currentWeek-3)*24*60*60*1000; //星期三
	var thursday = now.getTime() -(currentWeek-4)*24*60*60*1000; //星期四
	var friday  =now.getTime() - (currentWeek-5)*24*60*60*1000; //星期五
	var saturday = now.getTime() -(currentWeek-6)*24*60*60*1000; //星期六
	var sunday = now.getTime() +(7-currentWeek)*24*60*60*1000;     //星期日
 
	week=weektoday(monday,tuesday,wednesday,thursday,friday,saturday,sunday);
 
	return week; 
}
//获得上周的周一和周末
function getLastWeekDate(){
	var now = new Date();
	var week=new Array();
	var currentWeek = now.getDay();
 
	if ( currentWeek == 0 ){
	currentWeek = 7;
	}
	var monday  =now.getTime() - (currentWeek+6)*24*60*60*1000; //星期一
	var tuesday  =now.getTime() - (currentWeek+5)*24*60*60*1000; //星期二
	var wednesday = now.getTime() -(currentWeek+4)*24*60*60*1000; //星期三
	var thursday = now.getTime() -(currentWeek+3)*24*60*60*1000; //星期四
	var friday  =now.getTime() - (currentWeek+2)*24*60*60*1000; //星期五
	var saturday = now.getTime() -(currentWeek+1)*24*60*60*1000; //星期六
	var sunday  =now.getTime() -(currentWeek)*24*60*60*1000;  //星期日
 
	week=weektoday(monday,tuesday,wednesday,thursday,friday,saturday,sunday);
 
	return week; 
 
}
//获得下周的周一至周末
function getNextWeekDate(){
	var now = new Date();
	var week=new Array();
 
	var currentWeek = now.getDay();
 
	if ( currentWeek == 0 )	{
		currentWeek = 7;
	}
	var monday = now.getTime() -(currentWeek-8)*24*60*60*1000;  //星期一
	var tuesday= now.getTime() -(currentWeek-9)*24*60*60*1000;  //星期二
	var wednesday = now.getTime() -(currentWeek-10)*24*60*60*1000; //星期三
	var thursday = now.getTime() -(currentWeek-11)*24*60*60*1000; //星期四
	var friday= now.getTime() -(currentWeek-12)*24*60*60*1000; //星期五
	var saturday = now.getTime() -(currentWeek-13)*24*60*60*1000; //星期六
	var sunday = now.getTime() -(currentWeek-14)*24*60*60*1000; //星期日
 
 
	week=weektoday(monday,tuesday,wednesday,thursday,friday,saturday,sunday);
 
	return week; 
 
}
function weektoday(monday,tuesday,wednesday,thursday,friday,saturday,sunday){
 
	var days=new Array();
 
	var new_monday = changeTime(new Date(monday));//星期一
	var new_tuesday= changeTime(new Date(tuesday)); //星期二
	var new_wednesday = changeTime(new Date(wednesday)); //星期三
	var new_thursday = changeTime(new Date(thursday)); //星期四
	var new_friday= changeTime(new Date(friday)); //星期五
	var new_saturday = changeTime(new Date(saturday));//星期六
	var new_sunday = changeTime(new Date(sunday));//星期日
 
	days[0]=new_monday;
	days[1]=new_tuesday;
	days[2]=new_wednesday;
	days[3]=new_thursday;
	days[4]=new_friday;
	days[5]=new_saturday;
	days[6]=new_sunday;
 
	return days;
 
}
 
function changeTime(str){
	var fullDate = "";
	var tmpMonth = "";
	var tmpDay = "";
	var tmpYear = "";
	tmpMonth = str.getMonth() + 1;
	if(tmpMonth < 10){
		tmpMonth = "0" + tmpMonth;
	}
	tmpDay = str.getDate();
	if(tmpDay < 10){
		tmpDay = "0" + tmpDay;
	}
	tmpYear = str.getFullYear();
	return(tmpMonth+"/"+tmpDay+"/"+tmpYear);
} 
 
function getDayOfWeek(){
	var myDate = document.getElementById("mydate").value;
	var temp = document.getElementById("tmp").value;	
	var tmpMonth = "";
	var tmpDay = "";
	var tmpYear = "";
	tmpMonth = myDate.substring(0,2);
	tmpday = myDate.substring(3,5);
	tmpYear = myDate.substring(6,10);
	var now = new Date(tmpYear,tmpMonth-1,tmpday);	
	var currentWeek = now.getDay();	
	if ( currentWeek == 0 )	{
		currentWeek = 7;
	}
	var weekDay;
	temp = temp.toUpperCase();
	if (temp.indexOf("M")==0) {
		weekDay = 1;
	} else if (temp.indexOf("T")==0) {
		weekDay = 2;
	} else if (temp.indexOf("M")==0) {
		weekDay = 3;
	} else if (temp.indexOf("H")==0) {
		weekDay = 4;
	} else if (temp.indexOf("F")==0) {
		weekDay = 5;
	} else if (temp.indexOf("S")==0) {
		weekDay = 6;
	} else if (temp.indexOf("U")==0) {
		weekDay = 7;
	}
	var weeks = temp.substring(1);
	var days = weeks * 7 + weekDay;
 
	var weekday = now.getTime() -(currentWeek-days)*24*60*60*1000; 
	var formatDate = new Date(weekday);
	document.getElementById("newdate").value=changeTime(formatDate);
}
 
function getNextDay(){
	var strDate = document.getElementById("tmpdate").value;
	var temp = document.getElementById("days").value;	
	if("" == strDate || "" == temp){
		return "";
	} else {
		var tmpMonth = "";
		var tmpDay = "";
		var tmpYear = "";
		tmpMonth = strDate.substring(0,2);
		tmpday = strDate.substring(3,5);
		tmpYear = strDate.substring(6,10);
		var date = new Date(tmpYear,tmpMonth-1,tmpday);
		//alert(date);
		temp = new Number(temp);
		date.setDate(date.getDate() + temp);
		//alert(date);
		tempStr = changeTime(date);
		document.getElementById("newdate1").value=tempStr;
	}
}
</script>
<body>
<form name="form1" id="form1">
  <table>
    <tr>
      <td>星期一</td>
      <td><input type="text" value="" name="monday" id="monday"></td>
      <td>星期二</td>
      <td><input type="text" value="" name="tuesday" id="tuesday"></td>
      <td>星期三</td>
      <td><input type="text" value="" name="wednesday" id="wednesday"></td>
    </tr>
    <tr>
      <td>星期四</td>
      <td><input type="text" value="" name="thursday" id="thursday"></td>
      <td>星期五</td>
      <td><input type="text" value="" name="friday" id="friday"></td>
      <td>星期六</td>
      <td><input type="text" value="" name="saturday" id="saturday"></td>
    </tr>
    <tr>
      <td>星期日</td>
      <td><input type="text" value="" name="sunday" id="sunday"></td>
    </tr>
  </table>
  <table>
    <tr>
      <td><input type="button" value="本周" onClick="setWeekTime('this')"></td>
      <td><input type="button" value="上周" onClick="setWeekTime('last')"></td>
      <td><input type="button" value="下周" onClick="setWeekTime('next')"></td>
    </tr>
  </table>
 
  <table>
  <tr>
      <td colspan="4">根据时间日期获下一个周几的时间<br>
      第一个框是输入日期;<br>
      第二个框输入:<br>
      M (Mon) T (Tue) W (Wed) H (Thu) F (Fri) S (Sat) U (Sun)<br>
M1代表一周后的周一,M2两周后的周一</td>
    </tr>
    <tr>
      <td><input type="text" value="07/22/2010" name="mydate" id="mydate"></td>
      <td><input type="text" value="m1" name="tmp" id="tmp"></td>
      <td><input type="text" value="" name="newdate" id="newdate"></td>
      <td><input type="button" value="获得时间" onClick="getDayOfWeek()"></td>
    </tr>
  </table>
 
  <table>
  <tr>
      <td colspan="4">根据时间日期获取几天后的日期<br>
      第一个框是输入日期;<br>
      第二个框输入一个数字,表示几天后</td>
    </tr>
    <tr>
      <td><input type="text" value="07/22/2010" name="tmpdate" id="tmpdate"></td>
      <td><input type="text" value="6" name="days" id="days"></td>
      <td><input type="text" value="" name="newdate1" id="newdate1"></td>
      <td><input type="button" value="获得时间" onClick="getNextDay()"></td>
    </tr>
  </table>
 
</form>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics