英文:
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:
<!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() + 1, 4, 31); // May 31st of next year
if (today.getMonth() + 1 >= 12 || today.getMonth() + 1 <= 5) {
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() + 1, 4, 31); // May 31st of next year
if (today.getMonth() + 1 < 12 && today.getMonth() + 1 > 5) {
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论