日期:2014-05-16  浏览次数:20400 次

JavaScript Date对象方法收集
1,获得前天的日期
  
 var nowDate = new Date();
   var preDate = new Date(nowDate.valueOf()-2*24*60*60*1000);


2,将日期对象转换为一定格式的字符串
  
dateFormatToString=function(date,retV){
		//parse month
			if(retV.indexOf("MM")!=-1){
						var m = date.getMonth()+1;
						m = m<10?"0"+m:m;
					    retV = retV.replace(/MM/g,m);
					}
					retV = retV.toLowerCase();
					//parse year
					
					if(retV.indexOf("yyyy")!=-1){
					    retV = retV.replace(/yyyy/g,date.getFullYear());
					}else if(retV.indexOf("yy")!=-1){
					    var year4=date.getFullYear();
    					var year2=year4.toString().substring(2);
						retV = retV.replace(/yy/g,year2);
					}
					//parse day
					if(retV.indexOf("dd")!=-1){
						var d = date.getDate();
						d = d<10?"0"+d:d;
					    retV = retV.replace(/dd/g,d);
					}
					//parse hours
					if(retV.indexOf("hh")!=-1){
						var h = date.getHours();
						h = h<10?"0"+h:h;
					    retV = retV.replace(/hh/g,h);
					}
					//parse minute
					if(retV.indexOf("mm")!=-1){
						var mm = date.getMinutes();
						mm = mm<10?"0"+mm:mm;
					    retV = retV.replace(/mm/g,mm);
					}
					//parse second
					if(retV.indexOf("ss")!=-1){
						var s = date.getSeconds();
						s = s<10?"0"+s:s;
					    retV = retV.replace(/ss/g,s);
					}
			return retV;	
	};


3,获得本周星期一的日期
var Nowdate = new Date();
  var WeekFirstDay = new Date(Nowdate-(Nowdate.getDay()-1)*86400000); 
 

4,获得本周星期天的日期
 
var   Nowdate=new   Date();   
  var   WeekFirstDay=new   Date(Nowdate-(Nowdate.getDay()-1)*86400000);   
  var   WeekLastDay=new   Date((WeekFirstDay/1000+6*86400)*1000); 


5,获得本月第一天的日期
var   Nowdate=new   Date();   
  var   MonthFirstDay=new   Date(Nowdate.getYear(),Nowdate.getMonth(),1); 
 

6,获得本月最后一天的日期
var   Nowdate=new   Date();   
  var   MonthNextFirstDay=new   Date(Nowdate.getYear(),Nowdate.getMonth()+1,1);   
  var   MonthLastDay=new   Date(MonthNextFirstDay-86400000);