JavaScript根据用户的当前时间输出文本

时间:2012-11-06 05:29:34

标签: javascript string text time

我不懂JavaScript,但我熟悉以下说明。我知道一点PHP。

我正在寻找一块JS,它将根据用户的当前时间为我的标题输出特定的文本字符串。

例如:

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

15 个答案:

答案 0 :(得分:40)

尝试以下Javascript代码:

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

答案 1 :(得分:8)

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

演示:http://jsfiddle.net/DerekL/we8Ty/

答案 2 :(得分:3)

我知道这是一个旧线程,我只是向需要简单解决方案的任何人共享这两行代码:)

var hour = new Date().getHours();
console.log("Good " + (hour<12 && "Morning" || hour<18 && "Afternoon" || "Evening"))

答案 3 :(得分:2)

怎么样:

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'

或者补充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

答案 4 :(得分:1)

试试这个js代码,这应该有用..

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

答案 5 :(得分:0)

我没有测试过但它应该可以工作:

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

使用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';
}

改编自James1x0's gist

答案 8 :(得分:0)

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

    if (curHr < 12) {
        console.log('good morning')
    } else if (curHr >= 12 && curHr < 16) {
        console.log('good afternoon')
    } else {
        console.log('good evening')
    }

答案 9 :(得分:0)

var day=new Date();
var hr=day.getHours();
ReactDOM.render(
<div>
<h1>
{"Good"+ (hr< 12 ? "Morning": hr < 18 ? "Afternoon" :"Evening")}
</h1></div>,document.getElementById("root")); 

答案 10 :(得分:0)

这就是我用的

import React from "react";
import ReactDOM from "react-dom"

const today = new Date().getHours();

let greeting;

if (today < 12) {
  greeting = "Good Morning"
} else if (today < 18) {
  greeting = "Good Afternoon"
} else {
  greeting = "Good Night"
}

答案 11 :(得分:0)

ES6和Moment.js的另一种方法

  import moment from 'moment';

  const getGreetingTime = () => {
    const splitMorning = 6;
    const splitAfternoon = 12;
    const splitEvening = 17;
    const splitNight = 20;
    const currentHour = parseFloat(moment().format('HH'));
    if (currentHour >= splitMorning && currentHour <= splitAfternoon) {
      return 'Good morning';
    } else if (currentHour >= splitAfternoon && currentHour <= splitEvening) {
      return 'Good afternoon';
    } else if (currentHour >= splitEvening && currentHour <= splitNight) {
      return 'Good evening';
    }
    return 'Good night';
  };

  const greeting = getGreetingTime();

答案 12 :(得分:0)

试试这个

if (hour >= 0 && hour < 3){
                return 'Good Late Night!';
            }
            else if (hour >= 3 && 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 <= 15){
                return 'Good Noon!';
            }
            else if (hour > 15 && hour <= 6){
                return 'Good Afternoon!';
            }
            else if (hour > 6 && hour < 12){
                return 'Good Evening';
            }

答案 13 :(得分:0)

const date = new Date();
const currentTime = date.getHours();

let greeting;

if (currentTime >= 0 && currentTime <= 12) {
  greeting = "Good Morning";
} else if (currentTime > 12 && currentTime <= 18) {
  greeting = "Good Afternoon";
} else {
  greeting = "Good Evening";
}

ReactDOM.render(
  <h1>{greeting}</h1>,
  document.getElementById("root")
);

答案 14 :(得分:-1)

此处位于html文件中。 (您可以将脚本放在main.js内的html之外)

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <title>greating</title>
        <meta name='viewport' content='width=device-width, initial-scale=1'>
        <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
        <script src='main.js'></script>
    </head>
    <body>
        <h4 id="greating" >Test - this will be changed by the script</h4>
        <script>
        var dt = new Date().getHours();
            if (dt >= 0 && dt <= 11){
             document.getElementById("greating").innerHTML='Good Morning'
            }else if (dt >= 12 && dt <= 17){
                document.getElementById("greating").innerHTML='Good Afternoon'
            }else {
                document.getElementById("greating").innerHTML='Good Evening'
            }
        </script>
    </body>
    </html>>