gpt4 book ai didi

javascript - 获取本月至 future 5 个月的数据

转载 作者:行者123 更新时间:2023-12-03 03:21:24 25 4
gpt4 key购买 nike

我们有 2 个列表元素。

来自:(必须显示本月到接下来的 4 个月)例如

<li>October 2017</li> 
<li>November 2017</li>
<li>December 2017</li>
<li>January 2018</li>
<li>Febraury 2018</li>

致:(必须显示从本月到过去 4 个月的 1 年)例如

<li>June 2018</li> 
<li>July 2018</li>
<li>August 2018</li>
<li>September 2018</li>
<li>October 2018</li>

我们是否可以使用 Javascript 或 Jquery 显示它。

JS:

getFullMonth() {
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

var d = new Date();
var n = month[d.getMonth()];
document.getElementById("fromDate").innerHTML = n;
}

最佳答案

要实现此目的,您可以使用基于当前月份的 for 循环并递增每次迭代,并将带有目标月份的 li 附加到相关的 ul,类似这样:

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var currentMonth = new Date().getMonth();

for (var i = 0; i < 5; i++) {
$('.from').append(`<li>${months[(currentMonth + i) % months.length]}</li>`);

$('.to').append(`<li>${months[(currentMonth + i + 8) % months.length]}</li>`);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="from"></ul>
<ul class="to"></ul>

关于javascript - 获取本月至 future 5 个月的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46562145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com