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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.