Home  >  Article  >  Web Front-end  >  Use Date object to complete, display different greetings according to different times on the page, such as: good morning, good noon, good afternoon, good evening and other information_html/css_WEB-ITnose

Use Date object to complete, display different greetings according to different times on the page, such as: good morning, good noon, good afternoon, good evening and other information_html/css_WEB-ITnose

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

2015-03-28 17:20:18

There are two solutions to this question:

The first one:

<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>

Solution to Chapter 2:

<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>

The second method is generally used to select points, and the first method is suitable for selecting ranges, so this method is used for this question For range questions, it’s best not to switch, troublesome

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