获取当前周下周上周日期

just posted @ 2012年11月04日 21:00 in web , 2628 阅读

 

/*获取yyyy-mm-dd day天的日期数组
* getDays('2012-11-01',2)
* 返回['2012-11-01','2012-11-02']
*/
function getDays(str, day){
  var   dd, mm, yy;   
  var   reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
  if (arr = str.match(reg)) {
    yy = Number(arr[1]);
    mm = Number(arr[2])-1;
    dd = Number(arr[3]);
  } else {
    var d = new Date();
    yy = d.getUTCFullYear();
    mm = ("00"+(d.getUTCMonth())).slice(-2);
    dd = ("00"+d.getUTCDate()).slice(-2);
  }
  var aa=[];
  if(day>=0){
  	for(var i=0;i<day;i++){
  	  aa.push(_getDate(yy, mm, dd, i));
    }
  }else{
    for(var i=(day+1);i<=0;i++){
  	  aa.push(_getDate(yy, mm, dd, i));
    }	
  }
  
  //alert(aa);
  return aa;
}
/*
 *返回n天后的日期
 * get_day("2012-11-01",3) 返回 "2012-11-04"
 * 
 */
function get_day(str, n){
	  var   dd, mm, yy;   
	  var   reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
	  if (arr = str.match(reg)) {
	    yy = Number(arr[1]);
	    mm = Number(arr[2])-1;
	    dd = Number(arr[3]);
	  } else {
	    var d = new Date();
	    yy = d.getUTCFullYear();
	    mm = ("00"+(d.getUTCMonth())).slice(-2);
	    dd = ("00"+d.getUTCDate()).slice(-2);
	  }
	  
	  var aa=_getDate(yy, mm, dd, n);
	  
	  return aa;
	}

/*
 * 获取day天后的日期
 * 例如_getDate(2012,11,1,2):获取2012-11-01后2天的日期
        例如_getDate(2012,11,1,-2):获取2012-11-01前2天的日期
 */

function _getDate(yy, mm, dd, day) {
    var s, d, t, t2;
    t = Date.UTC(yy, mm, dd);
    t2 = day * 1000 * 3600 * 24; //加减day天的时间
    t+= t2;
    d = new Date(t);
    s = d.getUTCFullYear() + "-";
    s += ("00"+(d.getUTCMonth()+1)).slice(-2) + "-";
    s += ("00"+d.getUTCDate()).slice(-2);

    //alert(s);
    return s;
}

/*这个方法将取得 某日期(nowDate) 所在周的星期几(weekDay)的日期
 * getNextDate("2012-11-04",1); 2012-11-04所在周的星期一的日期
 * 返回2012-10-29
 * 
 */
function getNextDate(str,weekDay){
	  var   dd, mm, yy;   
	  var   reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
	  if (arr = str.match(reg)) {
	    yy = Number(arr[1]);
	    mm = Number(arr[2])-1;
	    dd = Number(arr[3]);
	  }
	  var t = Date.UTC(yy, mm, dd);
	  var d = new Date(t);
		weekDay%=7;
		var day = d.getDay();
		if(day==0)day=7;//自己加的 否则如果是周日 day=0  我取所在周一的日期,返回的是下周的
		var time = d.getTime();
		var sub = weekDay-day;
		time+=sub*24*3600000;
		d.setTime(time);
		var s = d.getUTCFullYear() + "-";
    s += ("00"+(d.getUTCMonth()+1)).slice(-2) + "-";
    s += ("00"+d.getUTCDate()).slice(-2);
    return s;
}
function getNextDate1(str,weekDay){
	  var d=new Date(str);
		weekDay%=7;
		var day = d.getDay();
		if(day==0)day=7;//自己加的 否则如果是周日 day=0  我取所在周一的日期,返回的是下周的
		var time = d.getTime();
		var sub = weekDay-day;
		time+=sub*24*3600000;
		d.setTime(time);
		var s = d.getUTCFullYear() + "-";
    s += ("00"+(d.getUTCMonth()+1)).slice(-2) + "-";
    s += ("00"+d.getUTCDate()).slice(-2);
    
    return s;
}
/*
 * 参数 2012-11-08
 * 返回周几
 */
function week(dd){
	dd=dd.replace(/\-/g,"/");
	var week="日一二三四五六".charAt(new Date(dd).getDay()); 
	return week;
}
/*
 * 获取当前日期
 */
function getCurrDay(){
	var d = new Date();
    yy = d.getUTCFullYear();
    mm = ("00"+(d.getUTCMonth()+1)).slice(-2);
    dd = ("00"+d.getUTCDate()).slice(-2);
    return yy+"-"+mm+"-"+dd;
}

 

 

 

<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<SCRIPT type=text/javascript src="week_day.js"></SCRIPT>
<script type="text/javascript">
	
function getWeek(flag){
	var start_day="";
	var curr_day=getCurrDay();
	var mon_day=getNextDate(curr_day,1);//当前日期所在周星期一的日期
	if(flag=='本周'){//查询本周
		start_day=mon_day;//当前时间
		d=getDays(start_day,7);
	}else if(flag=='下周'){//下周
		start_day=get_day(mon_day,7);//获取7天后的开始日期
		d=getDays(start_day,7);
		
	}else if(flag=='下下周'){//下下周
		start_day=get_day(mon_day,14);//获取14天后的开始日期
		d=getDays(start_day,7);
	}else if (flag=='上周'){
	  	start_day=get_day(mon_day,-7);//获取7天后的开始日期
		  d=getDays(start_day,7);
	}
	alert(d);
}

</script>
		
	</head>
<body>
<button onclick="getWeek('上周')">上周</button>
<button onclick="getWeek('本周')">本周</button>
<button onclick="getWeek('下周')">下周</button>
<button onclick="getWeek('下下周')">下下周</button>
</body>
</html>

 

 

  • 无匹配
  • 无匹配
Law Essay Help UK 说:
2018年11月06日 09:12

Incredible article! I am picking up from it. It's captivating and a marvellous substance. That is some extraordinary points of view with respect to this subject.

spanish to english 说:
2018年12月12日 11:17

I have a similar issue. I will try your solution and see if it works

seo service UK 说:
2024年1月16日 15:24

Excellent points?I would notice that as someone who in reality doesn’t write on blogs a lot (actually, this can be my first publish), I don’t assume th


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter