JavaScript to Output Text Based on User's Current Time

38,340

Solution 1

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')
}

Solution 2

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/

Solution 3

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

Solution 4

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

Solution 5

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

Share:
38,340
Mike Meldrem
Author by

Mike Meldrem

Updated on October 27, 2021

Comments

  • Mike Meldrem
    Mike Meldrem over 2 years

    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!
    
  • Derek 朕會功夫
    Derek 朕會功夫 over 11 years
    What if you have a lot of time and text? The code will be so messy.
  • Mike Meldrem
    Mike Meldrem over 11 years
    It's 12:21 by my computer clock, yet it said good morning. Is it possible that it only truncates the hours so, perhaps the array should say 0, 11? I have no idea, merely suggesting.
  • Mike Meldrem
    Mike Meldrem over 11 years
    This works for me. Of course I had to change console.log to document.write to get it to output to the page. I chose this because it is simplest and works for my needs, although some others' answers below may be more correct.
  • Derek 朕會功夫
    Derek 朕會功夫 over 11 years
    @MikeMeldrem - I'm pretty sure it should be [0,11]... No idea why I put [0,12] before.
  • Kapil Soni
    Kapil Soni almost 5 years
    sir its not working for me properly because at evening in my clock time is 7O'clock but its return me 6 from currHr so tell me why get 6?
  • FluffyKitten
    FluffyKitten almost 4 years
    Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it improves on the other existing answers and upvoted answers that this question already has.
  • Sushmit Sagar
    Sushmit Sagar almost 4 years
    It should be currentHour < splitEvening
  • FluffyKitten
    FluffyKitten over 3 years
    Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the problem described in the question, so that it is useful to other users with similar issues.
  • Jonathan
    Jonathan almost 3 years
    Welcome to StackOverflow! Please explain your solution.
  • Sisir
    Sisir over 2 years
    This is good but Derek's answer's design is more elegant and dynamic instead of hard coding 3-4 if cases