JavaScript根据时间交付展示。

huangapple go评论75阅读模式
英文:

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.

   &lt;html&gt; 
&lt;head&gt;
    &lt;title&gt;Greeting Message using JavaScript&lt;/title&gt; 
&lt;/head&gt;
&lt;body&gt;
    &lt;label id=&quot;lblGreetings&quot;&gt;&lt;/label&gt;
&lt;/body&gt;

&lt;script&gt;
    var myDate = new Date();
    var hrs = myDate.getHours();

    var greet;

    if (hrs &gt; 07 &amp;&amp; hrs &lt;= 19)
        greet = &#39;Today Evening&#39;;
    else if (hrs &gt;= 19 &amp;&amp; hrs &lt;= 24)
        greet = &#39;Tommorow Morning&#39;;
    else if (hrs &gt;= 00 &amp;&amp; hrs &lt;= 07)
        greet = &#39;Today Morning&#39;;

    document.getElementById(&#39;lblGreetings&#39;).innerHTML =
        &#39;&lt;b&gt;&#39; + greet + &#39;&lt;/b&gt; and welcome to Encodedna.com!&#39;;
&lt;/script&gt; 
&lt;/html&gt;

答案1

得分: 1

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

$(document).ready(function(){
    var myDate = new Date();
    var hrs = myDate.getHours();

    var greet;

    if (hrs &gt; 07 &amp;&amp; hrs &lt;= 19)
        greet = '今天晚上';
    else if (hrs &gt;= 19 &amp;&amp; hrs &lt;= 24)
        greet = '明天早上';
    else if (hrs &gt;= 00 &amp;&amp; hrs &lt;= 07)
        greet = '今天早上';

    document.getElementById('lblGreetings').innerHTML =
        '<b>' + greet + '</b>,欢迎访问Encodedna.com!';
});

&lt;!-- 语言: lang-html --&gt;

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">&lt;/script&gt;
<html&gt; 
<head&gt;
    <title&gt;使用JavaScript的问候消息&lt;/title&gt; 
</head&gt;
<body&gt;
    <label id="lblGreetings">&lt;/label&gt;
</body&gt;</html&gt;

&lt;!-- 结束代码片段 --&gt;
英文:

<!-- 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 &gt; 07 &amp;&amp; hrs &lt;= 19)
        greet = &#39;Today Evening&#39;;
    else if (hrs &gt;= 19 &amp;&amp; hrs &lt;= 24)
        greet = &#39;Tommorow Morning&#39;;
    else if (hrs &gt;= 00 &amp;&amp; hrs &lt;= 07)
        greet = &#39;Today Morning&#39;;

    document.getElementById(&#39;lblGreetings&#39;).innerHTML =
        &#39;&lt;b&gt;&#39; + greet + &#39;&lt;/b&gt; and welcome to Encodedna.com!&#39;;
 
 });

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;html&gt; 
&lt;head&gt;
    &lt;title&gt;Greeting Message using JavaScript&lt;/title&gt; 
&lt;/head&gt;
&lt;body&gt;
    &lt;label id=&quot;lblGreetings&quot;&gt;&lt;/label&gt;
&lt;/body&gt;&lt;/html&gt;

<!-- 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 &lt; 7) {
    greet = &#39;Today Morning&#39;;
} else if (hrs &lt; 19) {
    greet = &#39;Today Evening&#39;;
} else {
    greet = &#39;Tomorrow Morning&#39;;
}

alert(greet);

huangapple
  • 本文由 发表于 2020年1月3日 16:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定