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

js时间加减与时间对象与字符串之间的转换
//js时间的加法   
function timeAdd(){   
	//7天的毫秒数   加7天
	var interval = 7*24*60*60*1000;   
	var now = new Date();  
	now.setTime(now.getTime()+interval);   
}   
  
//将js的时间对象转换为字符串yyyyMMddHHssmm   
function jsTimeToString(time){   
	var year=time.getYear();   
	var month=time.getMonth()+1;   
	var day=time.getDate();   
	var hour=time.getHours();   
	var minute=time.getMinutes();   
	var second=time.getSeconds();   
	if(month<10){   
	    month="0"+month;   
	}   
	if(day<10){   
	    day="0"+day;   
	}   
	if(hour<10){   
	    hour="0"+hour;   
	}   
	if(minute<10){   
	    minute="0"+minute;   
	}   
	if(second<10){   
	    second="0"+second;   
	}   
	var strTime = year+""+month+""+day+""+hour+""+minute+""+second;    
return strTime;   
}      

//将字符串转换为js的时间对象,  字符串格式yyyyMMddHHssmm   
function stringToJsTime(time) {   
	var y = time.substring(0,4);   
	var m = time.substring(4,6)-1;   
	var d = time.substring(6,8);   
	var h = time.substring(8,10);   
	var mm = time.substring(10,12);   
	var ss = time.substring(12,14);   
	var date = new Date(y,m,d,h,mm,ss,0);   
	return date;   
}
 
//将字符串转换为js的时间对象,  字符串格式yyyy-MM-dd HH:ss:mm     
function stringToJsTime_(timeString) {    
	var y = timeString.substring(0,4);    
	var m = timeString.substring(5,7)-1;    
	var d = timeString.substring(8,10);    
	var h = timeString.substring(11,13);    
	var mm = timeString.substring(14,16);    
	var ss = timeString.substring(17,19);    
	var time = new Date(y,m,d,h,mm,ss,0);    
	return time;    
}