日期处理

当前日期 和一周前
GregorianCalendar cal = new GregorianCalendar();
			Date date_temp=new Date();
	        Date date = cal.getTime();
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
	        String bf_date = sdf.format(date);
            
	        Date end_date=new Date(new Date().getTime()-7*24*60*60*1000);
            
	        beforeDate=sdf.format(end_date);
	        afterDate=bf_date;




同理javascript处理日期时间也一样
		   var _date=new Date();
		   var bf_old_date;
		   var now_sys_nowDate;
		   var now_sys_date=(new Date()).toString();
		   
		   _date.setTime(_date.getTime()-7*24*3600*1000);
		   bf_old_date=new Date( _date.toString()).format('yyyy-MM-dd hh:mm:ss');
           now_sys_nowDate=new Date(now_sys_date).format('yyyy-MM-dd hh:mm:ss');


函数
	Date.prototype.format = function(format)
	{
		var o = {
		"M+" : this.getMonth()+1, //month
		
		"d+" : this.getDate(), //day
		
		"h+" : this.getHours(), //hour
		
		"m+" : this.getMinutes(), //minute
		
		"s+" : this.getSeconds(), //second
		
		"q+" : Math.floor((this.getMonth()+3)/3), //quarter
		
		"S" : this.getMilliseconds() //millisecond
		
		}
		if(/(y+)/.test(format))
		format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
		for(var k in o)
		if(new RegExp("("+ k +")").test(format))
		format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));
		return format;
	}









你可能感兴趣的:(JavaScript,prototype)