JS 获取当前时间

<script type="text/javascript">
getDay(0);//当天日期 某年某月某日 几时几分几秒 星期几
$('#date').text(getDay(0));
function getDay(day){  
	var today = new Date();  
	var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;          
	today.setTime(targetday_milliseconds); //注意,这行是关键代码

	var tYear = today.getFullYear();  
	var tMonth = today.getMonth();  
	var tDate = today.getDate();  
	var tHour = today.getHours();           
	var tMinute = today.getMinutes();           
	var tSecond = today.getSeconds();         
	var tDay = today.getDay();
	var weekday = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
	tMonth = doHandleMonth(tMonth + 1);  
	tDate = doHandleMonth(tDate);  
	if(tHour < 10){
	   tHour = "0"+tHour;
	} 
	if(tMinute < 10){
	   tMinute = "0"+tMinute;
	} 
	if(tSecond < 10){
	   tSecond = "0"+tSecond;
	}

	return tYear+"年"+tMonth+"月"+tDate+"日"+" "+tHour+":"+tMinute+":"+tSecond+" "+weekday[tDay];
}  
function doHandleMonth(month){  
	var m = month;  
	if(month.toString().length == 1){  
		m = "0" + month;  
	}  
	return m;  
}
</script>

 

<script type="text/javascript">
window.setInterval(getDateTime,1000); 
function getDateTime(){  
	var today = new Date();  

	var tYear = today.getFullYear();  
	var tMonth = today.getMonth();  
	var tDate = today.getDate();  
	var tHour = today.getHours();           
	var tMinute = today.getMinutes();           
	var tSecond = today.getSeconds();         
	var tDay = today.getDay();
	var weekday = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
	tMonth = doHandleMonth(tMonth + 1);  
	tDate = doHandleMonth(tDate);  
	if(tHour < 10){
	   tHour = "0"+tHour;
	} 
	if(tMinute < 10){
	   tMinute = "0"+tMinute;
	} 
	if(tSecond < 10){
	   tSecond = "0"+tSecond;
	} 
	var thisTime = tYear+"年"+tMonth+"月"+tDate+"日"+" "+tHour+":"+tMinute+":"+tSecond+" "+weekday[tDay];
	$('#date').text(thisTime);
}  
function doHandleMonth(month){  
	var m = month;  
	if(month.toString().length == 1){  
		m = "0" + month;  
	}
	return m;  
}
</script>

 

posted @ 2018-08-21 14:50  格鲁特baby  阅读(51)  评论(0编辑  收藏  举报