获取两个日期之间的所有日期,并仅返回该月的第一天
Get all dates between two dates and return only the first of the month
>我有一些代码可以返回两个预定义日期之间的所有日期。这非常好,但我想知道我怎么只能返回与每月第一天对应的值。
这样我就可以得到以下期望的结果:
Mon Feb 01 2016 01:00:00 GMT+0100 (W. Europe Standard Time)
Tue Mar 01 2016 01:00:00 GMT+0100 (W. Europe Standard Time)
Fri Apr 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Sun May 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Wed Jun 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Fri Jul 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Mon Aug 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Thu Sep 01 2016 01:00:00 GMT+0200 (W. Europe Daylight Time)
Tue Nov 01 2016 01:00:00 GMT+0100 (W. Europe Standard Time)
我的JS代码:
$('#getBetween').on('click', function () {
var start = new Date("2016-01-01"),
end = new Date("2016-12-01"),
currentDate = new Date(start),
between = []
;
while (currentDate <= end) {
between.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
$('#results').html(between.join('<br> '));
});
在这里演示
我需要创建哪种方法才能分配当月的第一天。
您可以
简单地构造一个新的Date
对象,同时向其添加一个月。以下是其中的片段:
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
因此,currentDate
选取前一个值的年份,在构造新的 Date 对象时将前一个值添加一个月,并将日期设置为 1(以确保您有第一天)。通过使用这种方式,您可以防止不必要的循环(例如从 1 月的第 2 天> 31 天)
$('#getBetween').on('click', function () {
var start = new Date("2016-01-01"),
end = new Date("2016-12-01"),
currentDate = new Date(start),
between = []
;
while (currentDate <= end) {
between.push(new Date(currentDate));
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
}
$('#results').html(between.join('<br> '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="getBetween">Get Between Dates</button>
<div id="results"></div>
如果结束日期在不同的年份,这也有效。
$('#getBetween').on('click', function () {
var start = new Date("2016-01-01"),
end = new Date("2017-06-01"), // end date is now mid 2017
currentDate = new Date(start),
between = []
;
while (currentDate <= end) {
between.push(new Date(currentDate));
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
}
$('#results').html(between.join('<br> '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="getBetween">Get Between Dates</button>
<div id="results"></div>
只需在 while 循环中替换:
currentDate.setDate(currentDate.getDate() + 1);
每:
currentDate.setMonth(currentDate.getMonth() + 1);
相关文章:
- "日期“;AJAX请求返回的类型值未定义
- 将客户端特定的日期格式返回到服务器MVC4
- JQuery UI日期选择器:在选择出发日期时自动弹出返回日期
- 它将返回什么新日期(DateObject)
- 对象数组返回自最近日期以来的最高总体值
- 从Parse.com返回的格式化日期
- PHP strtotime()为JavaScript日期字符串返回false
- date对象未在javascript中返回正确的日期值
- 下周末'javascript中的s datetime返回无效日期
- 不同格式的相同日期返回不同的值
- 为什么 IE 和 FF 不会为输入类型日期返回正确的属性或类型
- 日期选取器必须在最后一个选定日期返回
- Moment js没有在一周中的正确日期返回
- 获取两个日期之间的日期返回指定日期范围之外的错误日期
- 从解析数据库获取日期返回未定义
- javascript How '关键词影响'日期'返回值
- 为什么在解析时stringfy 'd moment.js日期返回不同的值?
- Javascript Date()对于某些日期返回错误的工作日
- javascript日期返回未定义
- Javascript日期返回错误的值