admin管理员组文章数量:1134247
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!
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!
Share
Improve this question
edited Nov 6, 2012 at 5:41
Sibu
4,6172 gold badges29 silver badges38 bronze badges
asked Nov 6, 2012 at 5:29
Mike MeldremMike Meldrem
5491 gold badge4 silver badges12 bronze badges
17 Answers
Reset to default 113Try 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')
}
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/
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;
}
}
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"))
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
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")
);
This should help you:
const currentTime = new Date().getHours();
let greetingText = "";
if (currentTime < 12) {
greetingText = "Good Morning";
} else if (currentTime < 18) {
greetingText = "Good Afternoon";
} else {
greetingText = "Good Evening";
}
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')
}
I have created a pure TypeScript function:
type SayHelloProps = {
morning: string;
afternoon: string;
evening: string;
};
const sayHello = ({
morning,
afternoon,
evening
}: SayHelloProps): string => {
const currentTime = new Date();
const currentHour = currentTime.getHours();
switch (true) {
case currentHour < 12:
return morning;
case currentHour > 12 && currentHour < 17:
return afternoon;
default:
return evening;
}
};
Readable, easy to use.
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';
}
<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>
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.
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')
}
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"));
This is what I used
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"
}
Another way with ES6 and 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();
Try this
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';
}
本文标签: stringJavaScript to Output Text Based on User39s Current TimeStack Overflow
版权声明:本文标题:string - JavaScript to Output Text Based on User's Current Time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736857729a1955786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论