Home  >  Article  >  Web Front-end  >  使用Date日期对象来完成,在页面上根据不同时间显示不同的问候语,如:早上好,中午好,下午好,晚上好等信息_html/css_WEB-ITnose

使用Date日期对象来完成,在页面上根据不同时间显示不同的问候语,如:早上好,中午好,下午好,晚上好等信息_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:47:133341browse

2015-03-28 17:20:18

此题有两种解法:

第一种:

<script>	var date=new Date();	var hour=date.getHours();	var str='';	if(hour<4){		str='凌晨好!';	}else if(hour<6){		str='早上好!';	}else if(hour<11){		str='上午好!';	}else if(hour<13){		str='中午好!';	}else if(hour<16){		str='下午好!';	}else if(hour<20){		str='晚上好!';	}else{		str='太晚了,休息吧!';	}	document.write('现在是'+hour+'点'+'<br>'+str);  </script>

  第二章解法:

<script>	var date=new Date();	var hour=date.getHours();	var str='';	switch(hour){	case 0:case 1:case 2:case 3:case 4:str='凌晨好!';break;	case 5:case 6:str='早上好!';break;	case 7: case 8:case 9:case 10:case 11:str='上午好!';break;	case 12:case 13:str='中午好!';break;	case 14:case 15:case 16:str='下午好!';break;	case 17:case 18:case 19:case 20:str='晚上好!';break;	case 23:case 22:case 21:str='太晚了,休息吧!';break;	}	document.write('现在是'+hour+'点'+'<br>'+str);  </script>

  第二种方法一般用于取点,第一种适合取范围,所以向本题这种取范围的题,最好不用switch,麻烦

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn