我希望通过使用Jquery获得财务年度,检查示例,因为我有12个字段,每个字段都有月份名称,在同一行中它还显示月份的第一个和最后一个日期,一些我如何达到关键但无法得到这样的结果。
var theMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
for (i = 0; i < 12; i++) {
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(theMonths[i]), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var months = '<li class="' + theMonths[i] + '">' + theMonths[i] + ' - - - - - - ' + firstDay + '---------' + lastDay + '</li>';
$('.calenderYear .month').append(months);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calenderYear">
<ul class="month"></ul>
<span class="dates"></span>
</div>
&#13;
答案 0 :(得分:0)
问题是因为您要将数组中的文本值提供给month
对象构造函数的Date
参数。该值应为整数。
因此,您的数组是多余的,您可以分别传递i
和i + 1
。
另请注意,您的输出将于7月开始,与财政年度相同。为此,您可以向迭代变量添加偏移量,并使用模运算符确保它包含到下一年的起始月份。
最后,lastDay
技术上应该是23:59:59
,而不是00:00:00
,否则将会有近24小时的时间,而且不会被覆盖。
试试这个:
var theMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var offset = 6;
for (i = 0; i < 12; i++) {
var date = new Date();
var monthIndex = i + offset;
var firstDay = new Date(date.getFullYear(), monthIndex % theMonths.length, 1);
var lastDay = new Date(date.getFullYear(), monthIndex + 1 % theMonths.length, 0, 23, 59, 59);
var months = '<li class="' + theMonths[i] + '"><p>' + theMonths[monthIndex % theMonths.length] + '</p><p>' + firstDay + '</p><p>' + lastDay + '</p></li>';
$('.calenderYear .month').append(months);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calenderYear">
<ul class="month"></ul>
<span class="dates"></span>
</div>
&#13;