JavaScript to Output Text Based on User's Curr

2020-02-10 02:40发布

I don't know JavaScript, but I am familiar with following directions. I know a little PHP.

I'm looking for a piece of JS that will output a particular string of text for my header, based on the user's current time.

For example:

12:00AM - 12:00PM - Good Morning!  
12:00PM - 6:00PM - Good Afternoon!  
6:00PM - 12:00AM - Good Evening!

9条回答
够拽才男人
2楼-- · 2020-02-10 03:05

I haven't tested but it should work:

function getText()
{
  var hour = new Date().getHours();
  var minute = new Date().getMinutes();
  if ( hour >= 0 && hour < 12 )
    return 'Good Morning!';
  else if ( hour == 12 && minute == 0 )
    return 'Good Morning!';
  else if ( hour == 12 && minute > 0 )
    return 'Good Afternoon!';
  else if ( hour == 6 && minute == 0 )
    return 'Good Afternoon!';
  else if ( hour == 6 && minute > 0 )
    return 'Good Evening!';
  else if ( hour > 12 && hour < 6 )
    return 'Good Afternoon!'; 
  else if ( hour > 6 && hour < 12 )
    return 'Good Evening';
}
查看更多
We Are One
3楼-- · 2020-02-10 03:09
<script type="text/javascript">

    var myDate = new Date();
    var name = window.prompt("Please enter your name: ");

    if (myDate.getHours() < 12) {
        document.write("Good Morning " +name);
    }
    else if(myDate.getHours() >=12 && myDate.getHours() <=17){
        document.write("Good Afternoon " +name);
    }
    else if (myDate.getHours() > 17 && myDate.getHours() <=24) {
        document.write("Good Evening " +name);
    }
    else
    {
        document.write("Good Night");
    }
</script>
查看更多
再贱就再见
4楼-- · 2020-02-10 03:15

Try following piece of Javascript code:

var today = new Date()
var curHr = today.getHours()

if (curHr < 12) {
  console.log('good morning')
} else if (curHr < 18) {
  console.log('good afternoon')
} else {
  console.log('good evening')
}
查看更多
登录 后发表回答