英文:
Javascript Delivery Show Based On Time
问题
以下是翻译好的部分:
我试图展示这个条件,
00 - 07 上午 - 显示 "今天早上" ||
07 - 19 下午 - 显示 "今天晚上" ||
19 - 24 上午 - 显示 "明天早上"
但我觉得我的代码有问题。
<html>
<head>
<title>使用JavaScript显示问候消息</title>
</head>
<body>
<label id="lblGreetings"></label>
</body>
<script>
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs > 07 && hrs <= 19)
greet = '今天晚上';
else if (hrs >= 19 && hrs <= 24)
greet = '明天早上';
else if (hrs >= 00 && hrs <= 07)
greet = '今天早上';
document.getElementById('lblGreetings').innerHTML =
'<b>' + greet + '</b> 欢迎来到Encodedna.com!';
</script>
</html>
英文:
I am trying to show this condition,
00 - 07 am - show "Today Morning" ||
07 - 19 pm - show "Today Evening" ||
19 - 24 am - show "Tomorrow Morning"
But I guess something is wrong with my code.
<html>
<head>
<title>Greeting Message using JavaScript</title>
</head>
<body>
<label id="lblGreetings"></label>
</body>
<script>
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs > 07 && hrs <= 19)
greet = 'Today Evening';
else if (hrs >= 19 && hrs <= 24)
greet = 'Tommorow Morning';
else if (hrs >= 00 && hrs <= 07)
greet = 'Today Morning';
document.getElementById('lblGreetings').innerHTML =
'<b>' + greet + '</b> and welcome to Encodedna.com!';
</script>
</html>
答案1
得分: 1
<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
$(document).ready(function(){
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs > 07 && hrs <= 19)
greet = '今天晚上';
else if (hrs >= 19 && hrs <= 24)
greet = '明天早上';
else if (hrs >= 00 && hrs <= 07)
greet = '今天早上';
document.getElementById('lblGreetings').innerHTML =
'<b>' + greet + '</b>,欢迎访问Encodedna.com!';
});
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
<title>使用JavaScript的问候消息</title>
</head>
<body>
<label id="lblGreetings"></label>
</body></html>
<!-- 结束代码片段 -->
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function(){
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs > 07 && hrs <= 19)
greet = 'Today Evening';
else if (hrs >= 19 && hrs <= 24)
greet = 'Tommorow Morning';
else if (hrs >= 00 && hrs <= 07)
greet = 'Today Morning';
document.getElementById('lblGreetings').innerHTML =
'<b>' + greet + '</b> and welcome to Encodedna.com!';
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
<title>Greeting Message using JavaScript</title>
</head>
<body>
<label id="lblGreetings"></label>
</body></html>
<!-- end snippet -->
答案2
得分: 0
以下是您的代码的翻译:
以下是您的代码的简化版本。请记住,您的陈述可以通过仅检查`hours`是否`低于`您的阈值来最小化。
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs < 7) {
greet = '今天早上';
} else if (hrs < 19) {
greet = '今天晚上';
} else {
greet = '明天早上';
}
alert(greet);
英文:
Below you can find a simplified version of your code. Keep in mind your statement can be minimised by only check if the hours
are lower than
your threshold.
var myDate = new Date();
var hrs = myDate.getHours();
var greet;
if (hrs < 7) {
greet = 'Today Morning';
} else if (hrs < 19) {
greet = 'Today Evening';
} else {
greet = 'Tomorrow Morning';
}
alert(greet);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论