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 02:55
var data = [
    [0, 4, "Good night"], 
    [5, 11, "Good morning"],          //Store messages in an array
    [12, 17, "Good afternoon"],
    [18, 24, "Good night"]
],
    hr = new Date().getHours();

for(var i = 0; i < data.length; i++){
    if(hr >= data[i][0] && hr <= data[i][1]){
        console.log(data[i][2]);
    }
}

Demo: http://jsfiddle.net/DerekL/we8Ty/

查看更多
家丑人穷心不美
3楼-- · 2020-02-10 02:57

Try this js code, This should work..

var dt = new Date().getHours();
if (dt >= 0 && dt <= 11){
 console.log('GM')
}else if (dt >= 12 && dt <= 17){
 console.log('Good Afternoon!')
}else {
 console.log('GE')
}
查看更多
smile是对你的礼貌
4楼-- · 2020-02-10 02:59

I know this is an old thread, i'm just sharing this 2 lines of code for anyone who needs simple solution :)

var hour = new Date().getHours();
console.log("Good " + (hour<12 && "Morning" || hour<18 && "Afternoon" || "Evening"))
查看更多
戒情不戒烟
5楼-- · 2020-02-10 03:01

This is just a small variation of the solution from Derek 朕會功夫 above.
I felt the array is cleaner than a bunch of if statements.
If you work the hours backwards, you don't need a start AND end hour.
Also, once you make a match, I added a break; to kick out early.

var data = [
    [22, 'Working late'],
    [18, 'Good evening'],
    [12, 'Good afternoon'],
    [5,  'Good morning'],
    [0,  'Whoa, early bird']
],
hr = new Date().getHours();
for (var i = 0; i < data.length; i++) {
    if (hr >= data[i][0]) {
        console.log(data[i][1])
        break;
    }
}
查看更多
Luminary・发光体
6楼-- · 2020-02-10 03:02

How about:

var time = new Date().getHours();
   ,greeting = 'Good '+ (time < 12 ? 'Morning' : 
                         time < 18 ? 'Afternoon' : 'Evening');
//=> new Date('2012/11/06 13:10') => 'Good Afternoon'
//=> new Date('2012/11/06 10:33') => 'Good Morning'
//=> new Date('2012/11/06 19:23') => 'Good Evening'

Or augment Date

Date.prototype.greeting = function(){ 
   var time = this.getHours();
   return 'Good '+ (time<12 ? 'Morning' : time<18 ? 'Afternoon' : 'Evening');
};
new Date('2012/11/06 19:23').greeting() //=> 'Good Evening'

see jsfiddle

查看更多
等我变得足够好
7楼-- · 2020-02-10 03:02

With moment.js & ES6:

import moment from 'moment';

console.log(`${getGreetingTime(moment())} ${userFirstName}`);

getGreetingTime = (currentTime) => {
  if (!currentTime || !currentTime.isValid()) { return 'Hello'; }

  const splitAfternoon = 12; // 24hr time to split the afternoon
  const splitEvening = 17; // 24hr time to split the evening
  const currentHour = parseFloat(currentTime.format('HH'));

  if (currentHour >= splitAfternoon && currentHour <= splitEvening) {
    return 'Good afternoon';
  } else if (currentHour >= splitEvening) {
    return 'Good evening';
  }
  return 'Good morning';
}

Adapted from James1x0's gist.

查看更多
登录 后发表回答