例如,如果在晚上 8 点到 1 点之间,它会说“早上好”,如果在下午 1:01-6:00 之间,它会说“下午好”,如果在 6:01-11 之间: 59、它会说“晚上好”
我该如何使用 javascript/jquery 来做到这一点?
最佳答案
您可以使用Date对象:
var date = new Date();
var hours = date.getHours();
if (hours >= 8 && hours < 13) {
alert('Good Morning');
} else if (hours >= 13 && hours < 18) {
alert('Good Afternoon');
} else if (hours > 18 && hours <= 23) {
alert('Good Evening');
} else {
alert('Don\'t disturb me at this lately hour');
}
关于javascript - 如何使用 javascript 根据一天中的时间显示不同的短语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3365300/