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

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

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Table Display Example</title>
  5. </head>
  6. <body>
  7. <!-- Display Table 1 during December 10th through May 31st -->
  8. <script>
  9. var today = new Date();
  10. var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
  11. var table1End = new Date(today.getFullYear(), 4, 31); // May 31st
  12. if (today >= table1Start && today <= table1End) {
  13. 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>");
  14. }
  15. </script>
  16. <!-- Display Table 2 during the rest of the days -->
  17. <script>
  18. var today = new Date();
  19. var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
  20. var table1End = new Date(today.getFullYear(), 4, 31); // May 31st
  21. if (today < table1Start || today > table1End) {
  22. 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>");
  23. }
  24. </script>
  25. </body>
  26. </html>

e
e
e
e
e
e
e
e
e
e
e

答案1

得分: 1

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>表格显示示例</title>
  5. </head>
  6. <body>
  7. <!-- 在12月10日至5月31日期间显示表格1 -->
  8. <script>
  9. var today = new Date();
  10. var table1Start = new Date(today.getFullYear(), 11, 10); // 12月10日
  11. var table1End = new Date(today.getFullYear() + 1, 4, 31); // 明年5月31日
  12. if (today.getMonth() + 1 >= 12 || today.getMonth() + 1 <= 5) {
  13. document.write("<table border='1'><tr><th>表格1</th></tr><tr><td>这是表格1,在12月10日至5月31日期间显示。</td></tr></table>");
  14. }
  15. </script>
  16. <!-- 在其他日期显示表格2 -->
  17. <script>
  18. var today = new Date();
  19. var table1Start = new Date(today.getFullYear(), 11, 10); // 12月10日
  20. var table1End = new Date(today.getFullYear() + 1, 4, 31); // 明年5月31日
  21. if (today.getMonth() + 1 < 12 && today.getMonth() + 1 > 5) {
  22. document.write("<table border='1'><tr><th>表格2</th></tr><tr><td>这是表格2,在其他日期显示。</td></tr></table>");
  23. }
  24. </script>
  25. </body>
  26. </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:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Table Display Example&lt;/title&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7. &lt;!-- Display Table 1 during December 10th through May 31st --&gt;
  8. &lt;script&gt;
  9. var today = new Date();
  10. var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
  11. var table1End = new Date(today.getFullYear() + 1, 4, 31); // May 31st of next year
  12. if (today.getMonth() + 1 &gt;= 12 || today.getMonth() + 1 &lt;= 5) {
  13. 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;);
  14. }
  15. &lt;/script&gt;
  16. &lt;!-- Display Table 2 during the rest of the days --&gt;
  17. &lt;script&gt;
  18. var today = new Date();
  19. var table1Start = new Date(today.getFullYear(), 11, 10); // December 10th
  20. var table1End = new Date(today.getFullYear() + 1, 4, 31); // May 31st of next year
  21. if (today.getMonth() + 1 &lt; 12 &amp;&amp; today.getMonth() + 1 &gt; 5) {
  22. 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;);
  23. }
  24. &lt;/script&gt;
  25. &lt;/body&gt;
  26. &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:

确定