年月日,星期,上中午的区分,时分秒,在一进入一个页面就显示年月日时分秒

(1)

<htmtl>
<head></head>
<body> 
<div id="linkweb" style=" display:inline;"></div> 
<script> 
  setInterval("document.getElementById('linkweb').innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay  ());",1000); 
</script> 
</body>
	</html>

结果:

(2)

<html>
<head>
<meta charset="utf-8">

<script>

	
	function showTime() { 
  var now = new Date(); 
  var nowTime = now.toLocaleString(); 
  var date = nowTime.substring(0,10);//截取日期 
  var time = nowTime.substring(10,20); //截取时间 
  var week = now.getDay(); //星期 
  var hour = now.getHours(); //小时 
  //判断星期几 
  var weeks = ["日","一","二","三","四","五","六"]; 
  var getWeek = "星期" + weeks[week]; 
  var sc; 
  //判断是AM or PM 
  if(hour >= 0 && hour < 5){ 
   sc = '凌晨'; 
  } 
  else if(hour > 5 && hour <= 7){ 
   sc = '早上'; 
  } 
  else if(hour > 7 && hour <= 11){ 
   sc = '上午'; 
  } 
  else if(hour > 11 && hour <= 13){ 
   sc = '中午'; 
  } 
  else if(hour> 13 && hour <= 18){ 
   sc = '下午'; 
  } 
  else if(hour > 18 && hour <= 23){ 
   sc = '晚上'; 
  } 
  document.getElementById('txt').innerHTML ="当前时间:" + date+" " + getWeek +" "+"   "+sc+"  "+time;
  setTimeout('showTime()',1000); 
} 
	
</script>
</head>
<body onload="showTime()">
	
<div id="txt"></div>
	
</body>
</html>

结果:




(3)在一进入一个页面就显示的年月日以及时分秒

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script>
function startTime(){
	var today=new Date();

var y=today.getFullYear();   //获取系统的年;
var M=today.getMonth()+1;   //获取系统月份,由于月份是从0开始计算,所以要加1
var d=today.getDate(); // 获取系统日,

	var h=today.getHours();
	var m=today.getMinutes();
	var s=today.getSeconds();// 在小于10的数字前加一个‘0’
	
	
	m=checkTime(m);
	s=checkTime(s);
	document.getElementById('txt').innerHTML=y+"/"+M+"/"+d+" "+h+":"+m+":"+s;
	t=setTimeout(function(){startTime()},500);
}
function checkTime(i){
	if (i<10){
		i="0" + i;
	}
	return i;
}
	
 
	
</script>
</head>
<body onload="startTime()">
	
<div id="txt"></div>
	
</body>
</html>

结果:








猜你喜欢

转载自blog.csdn.net/qq_26925297/article/details/80381994