2015-03-28 17:20:18
There are two methods of solving this problem:
The first type:
<script>var date=new Date (), Var hour=date.gethours (), Var str= ', if (hour<4) {str= ' Early morning good! ‘;} else if (hour<6) {str= ' Good morning! ‘;} else if (hour<11) {str= ' Good morning! ‘;} else if (hour<13) {str= ' noon good! ‘;} else if (hour<16) {str= ' Good afternoon! ‘;} else if (hour<20) {str= ' Good evening! ‘;} Else{str= ' It's too late, rest! ‘;} document.write (' Now is ' +hour+ ' point ' + ' <br> ' +str '); </script>
The second chapter solution:
<script>var date=new Date (), Var hour=date.gethours (), var str= '; switch (hour) {case 0:case 1:case 2:case 3:case 4: Str= ' Good morning! ' Break;case 5:case 6:str= ' Good morning! '; break;case 7:case 8:case 9:case 10:case 11:str= ' Good morning! '; break;case 12:case 13:str= ' noon good! '; break;case 14:case 15:case 16:str= ' Good afternoon! '; break;case 17:case 18:case 19:case 20:str= ' Good evening! '; break;case 23:case 22:case 21:str= ' It's too late, rest! '; break;} document.write (' Now is ' +hour+ ' point ' + ' <br> ' +str '); </script>
The second method is generally used to take the point, the first fit to take the range, so the question of this range of the problem, it is best not to switch, trouble
Use the date Date object to complete the display of different greetings on the page at different times, such as: Good morning, noon, good afternoon, good evening, etc. information