Changing parameters in .getFullYear() => 修改 .getFullYear() 中的参数

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

Changing parameters in .getFullYear()

问题

December should be 12 instead of 11, and May should be 5 instead of 4. January should start at 1 instead of 0.

英文:

So I want December to be 12 instead of 11 and May to be 5 instead of 4
So January starts at 1 instead of 0

<!DOCTYPE html>
<html>
<head>
	<title>Table Display Example</title>
</head>
<body>
	<!-- Display Table 1 during December 10th through May 31st -->
	<script>
		var today = new Date();
		var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
		var table1End = new Date(today.getFullYear(), 4, 31); // May 31st
		if (today >= table1Start && today <= table1End) {
			document.write("<table border='1'><tr><th>Table 1</th></tr><tr><td>This is Table 1, displayed during December 10th through May 31st.</td></tr></table>");
		}
	</script>

	<!-- Display Table 2 during the rest of the days -->
	<script>
		var today = new Date();
		var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
		var table1End = new Date(today.getFullYear(), 4, 31); // May 31st
		if (today < table1Start || today > table1End) {
			document.write("<table border='1'><tr><th>Table 2</th></tr><tr><td>This is Table 2, displayed during the rest of the days.</td></tr></table>");
		}
	</script>
</body>
</html>

e
e
e
e
e
e
e
e
e
e
e

答案1

得分: 1

在JavaScript中,'Date'构造函数中的月份从0开始,1代表一月。因此,要获取所需的值,您需要从五月和十二月的月份值中减去1,并在一月的月份值上加1。代码如下所示:

<!DOCTYPE html>
<html>
<head>
    <title>表格显示示例</title>
</head>
<body>
    <!-- 在12月10日至5月31日期间显示表格1 -->
    <script>
        var today = new Date();
        var table1Start = new Date(today.getFullYear(), 11, 10); // 12月10日
        var table1End = new Date(today.getFullYear() + 1, 4, 31); // 明年5月31日
        if (today.getMonth() + 1 >= 12 || today.getMonth() + 1 <= 5) {
            document.write("<table border='1'><tr><th>表格1</th></tr><tr><td>这是表格1,在12月10日至5月31日期间显示。</td></tr></table>");
        }
    </script>

    <!-- 在其他日期显示表格2 -->
    <script>
        var today = new Date();
        var table1Start = new Date(today.getFullYear(), 11, 10); // 12月10日
        var table1End = new Date(today.getFullYear() + 1, 4, 31); // 明年5月31日
        if (today.getMonth() + 1 < 12 && today.getMonth() + 1 > 5) {
            document.write("<table border='1'><tr><th>表格2</th></tr><tr><td>这是表格2,在其他日期显示。</td></tr></table>");
        }
    </script>
</body>
</html>
英文:

In JavaScript, the month in the 'Date' constructor starts at 0 for January. So, to get the values that you want, you need to subtract 1 from the month values for May and December, and add 1 to the month value for January. It would look something like this:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Table Display Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;!-- Display Table 1 during December 10th through May 31st --&gt;
    &lt;script&gt;
        var today = new Date();
        var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
        var table1End = new Date(today.getFullYear() + 1, 4, 31); // May 31st of next year
        if (today.getMonth() + 1 &gt;= 12 || today.getMonth() + 1 &lt;= 5) {
            document.write(&quot;&lt;table border=&#39;1&#39;&gt;&lt;tr&gt;&lt;th&gt;Table 1&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;This is Table 1, displayed during December 10th through May 31st.&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;);
        }
    &lt;/script&gt;

    &lt;!-- Display Table 2 during the rest of the days --&gt;
    &lt;script&gt;
        var today = new Date();
        var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
        var table1End = new Date(today.getFullYear() + 1, 4, 31); // May 31st of next year
        if (today.getMonth() + 1 &lt; 12 &amp;&amp; today.getMonth() + 1 &gt; 5) {
            document.write(&quot;&lt;table border=&#39;1&#39;&gt;&lt;tr&gt;&lt;th&gt;Table 2&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;This is Table 2, displayed during the rest of the days.&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;);
        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年4月17日 01:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029177.html
匿名

发表评论

匿名网友

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

确定