张晨的个人博客

JS获取系统当前时间及时间比较

张晨的个人博客2014-04-08前端技术 5237 0A+A-
一、获取系统时间
var now = new Date();    
now.getYear();        //获取当前年份(2位)    
now.getFullYear();    //获取完整的年份(4位,1970-????)    
now.getMonth();       //获取当前月份(0-11,0代表1月)    
now.getDate();        //获取当前日(1-31)    
now.getDay();         //获取当前星期X(0-6,0代表星期天)    
now.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)    
now.getHours();       //获取当前小时数(0-23)    
now.getMinutes();     //获取当前分钟数(0-59)    
now.getSeconds();     //获取当前秒数(0-59)    
now.getMilliseconds();    //获取当前毫秒数(0-999)    
now.toLocaleDateString();     //获取当前日期    
var time=now.toLocaleTimeString();     //获取当前时间    
now.toLocaleString( );        //获取日期与时间

//显示时间格式2014-4-8
year = now.getFullYear();		
month = now.getMonth() + 1;
day = now.getDate();
var today = year + "-" + month+ "-" + day;	
//为需要当前时间的地方赋值如 $("#time").val(today);
二、时间比较`
//校验输入日期是否是今天或者今天之前,输入的value为2014-04-09或2014/04/09格式
function checkDate(value){
	var now = new Date();
	if(Date.parse(value)-Date.parse(now)>0){
		alert('false');
	}else{
		alert('true');
	}
}
 
 
文章关键词
js
javascript
系统时间
发表评论