37

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

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

例如:

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

17 回答 17

88

尝试以下一段 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')
}
于 2012-11-06T05:40:37.870 回答
13
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/

于 2012-11-06T05:41:59.730 回答
7

这只是上面Derek 朕会功夫的解决方案的一个小变化。
我觉得数组比一堆if语句更干净。
如果您倒着工作时间,则不需要开始结束时间。
此外,一旦你进行了比赛,我添加了一个break;以提前踢出。

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;
    }
}
于 2016-12-05T16:00:30.757 回答
7

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

var hour = new Date().getHours();
console.log("Good " + (hour<12 && "Morning" || hour<18 && "Afternoon" || "Evening"))
于 2018-06-29T14:13:47.767 回答
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'

见 jsfiddle

于 2012-11-06T06:28:15.980 回答
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')
}
于 2012-11-06T05:41:02.573 回答
1
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")
);
于 2021-07-29T14:02:48.173 回答
1

这应该可以帮助您:

const currentTime = new Date().getHours();
let greetingText = "";

if (currentTime < 12) {
  greetingText = "Good Morning"; 
} else if (currentTime < 18) {
  greetingText = "Good Afternoon"; 
} else {
  greetingText = "Good Evening"; 
}
于 2021-10-26T13:34:36.750 回答
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';
}
于 2012-11-06T05:41:05.533 回答
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>
于 2014-12-23T10:16:58.637 回答
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 的要点

于 2018-10-09T09:51:45.843 回答
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')
    }
于 2020-05-04T07:33:17.903 回答
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")); 
于 2020-08-18T11:50:07.143 回答
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"
}
于 2020-08-26T23:15:33.303 回答
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();
于 2020-11-08T23:15:40.420 回答
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';
            }
于 2021-05-20T08:21:15.507 回答
-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" >ERROR</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>

于 2020-04-03T18:30:36.270 回答