Javascript 如何使用moment.js获取日期范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37673322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get date range using moment.js
提问by
This is my html page
这是我的 html 页面
<tr ng-show="option == 'Yearly' || option == 'Date'">
<td>
<label>From:</label>
</td>
<td>
<input type="text" ng-model="fromdate" id="from" date-picker date-type="from" month-directive />
{{fromdate}}
</td>
<td>
<label>To:</label>
</td>
<td>
<input id="todate" type="text" ng-model="todate" date-picker date-type="to" month-directive />
{{todate}}
</td>
</tr>
This is my directive
这是我的指示
app.directive('monthDirective', function () {
return {
restrict:'A',
link: function (scope, elem) {
var fromDate, toDate;
scope.$watch('fromdate', function (newValue, oldValue) {
fromDate = newValue;
console.log('fromDate', newValue);
});
scope.$watch('todate', function (newValue, oldValue) {
todate = newValue;
console.log('todate', newValue);
});
var range = moment().range(fromDate, toDate);
var diff = moment.preciseDiff(fromDate,toDate);
console.log('Range', range);
console.log('diff', diff);
}
}
})
})
I need to get the range between fromdate and todate using mument.js. Can anyone suggest how to do this in my scenario. Thanks in advance
我需要使用 mument.js 获取 fromdate 和 todate 之间的范围。任何人都可以建议如何在我的场景中做到这一点。提前致谢
采纳答案by sajalsuraj
To get rest of the dates between two dates, you can refer this code-
要获得两个日期之间的其余日期,您可以参考此代码-
$scope.dateArr = []; //Array where rest of the dates will be stored
$scope.prevDate = moment().subtract(15, 'days');//15 days back date from today(This is the from date)
$scope.nextDate = moment().add(15, 'days');//Date after 15 days from today (This is the end date)
//extracting date from objects in MM-DD-YYYY format
$scope.prevDate = moment($scope.prevDate._d).format('MM-DD-YYYY');
$scope.nextDate = moment($scope.nextDate._d).format('MM-DD-YYYY');
//creating JS date objects
var start = new Date($scope.prevDate);
var end = new Date($scope.nextDate);
//Logic for getting rest of the dates between two dates("FromDate" to "EndDate")
while(start < end){
$scope.dateArr.push(moment(start).format('ddd DD-MM'));
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
}
console.log('Dates:- ');
console.log($scope.dateArr);
This is the console log:-
这是控制台日志:-
["Tue 24-05", "Wed 25-05", "Thu 26-05", "Fri 27-05", "Sat 28-05", "Sun 29-05", "Mon 30-05", "Tue 31-05", "Wed 01-06", "Thu 02-06", "Fri 03-06", "Sat 04-06", "Sun 05-06", "Mon 06-06", "Tue 07-06", "Wed 08-06", "Thu 09-06", "Fri 10-06", "Sat 11-06", "Sun 12-06", "Mon 13-06", "Tue 14-06", "Wed 15-06", "Thu 16-06", "Fri 17-06", "Sat 18-06", "Sun 19-06", "Mon 20-06", "Tue 21-06", "Wed 22-06"]
[“周二 24-05”、“周三 25-05”、“周四 26-05”、“周五 27-05”、“周六 28-05”、“周日 29-05”、“周一 30-05”、 “周二 31-05”、“周三 01-06”、“周四 02-06”、“周五 03-06”、“周六 04-06”、“周日 05-06”、“周一 06-06”、“星期二 07-06”、“星期三 08-06”、“星期四 09-06”、“星期五 10-06”、“星期六 11-06”、“星期日 12-06”、“星期一 13-06”、“星期二” 14-06”、“星期三 15-06”、“星期四 16-06”、“星期五 17-06”、“星期六 18-06”、“星期日 19-06”、“星期一 20-06”、“星期二 21” -06", "星期三 22-06"]
回答by Quentin Roger
You can do something like this to get the difference in days:
你可以做这样的事情来获得天数的差异:
var fromDate = moment();
var toDate = moment().add(15, 'days');
var range = moment().range(fromDate, toDate);
var diff = range.diff('days');
var array = range.toArray('days');
$.each(array, function(i, e) {
$(".container").append("<li>" + moment(e).format("DD MM YYYY") + "</li>");
});
console.log('Range', range);
console.log('diff', diff);
EDIT
编辑
I added a little example for the toArray
function :
我为toArray
函数添加了一个小例子:
回答by oldo.nicho
Whilst not as fun as coming up with your own function, the easy answer is to use https://github.com/rotaready/moment-range
虽然不像提出自己的函数那么有趣,但简单的答案是使用https://github.com/rotaready/moment-range
回答by AuthorProxy
const getDaysRange = (date1, date2) => {
if (date1 && date2) {
return Math.abs(moment(date2).diff(moment(date1), 'days')) + 1;
}
return undefined;
};
回答by Adam Bubela
If you want to get consecutive days between two dates:
如果您想获得两个日期之间的连续天数:
- Install MomentJS and MomentRange:
npm i moment && npm i moment-range
- Some code
- 安装 MomentJS 和 MomentRange:
npm i moment && npm i moment-range
- 一些代码
const
Moment = require("moment"),
MomentRange = require("moment-range"),
moment = MomentRange.extendMoment(Moment); /*add plugin to moment instance*/
let
range = moment().range("2018-02-20", "2018-03-01"), /*can handle leap year*/
array = Array.from(range.by("days")); /*days, hours, years, etc.*/
array.map(m => {
console.log(m.format("YYYY-MM-DD")); /*or any format you like*/
});
回答by kiwicopple
I know this is an old/answered question, but since it's at the top of google here is how to achieve a date range with only moment
(and without moment-range
):
我知道这是一个旧的/已回答的问题,但由于它位于 google 的顶部,因此如何仅使用moment
(和不使用moment-range
)实现日期范围:
import moment from 'moment'
/**
* @param {date|moment} start The start date
* @param {date|moment} end The end date
* @param {string} type The range type. eg: 'days', 'hours' etc
*/
function getRange(startDate, endDate, type) {
let fromDate = moment(startDate)
let toDate = moment(endDate)
let diff = toDate.diff(fromDate, type)
let range = []
for (let i = 0; i < diff; i++) {
range.push(moment(startDate).add(i, type))
}
return range
}
Returns an array of every "type" (month/day/hour etc) between the two, exclusive of the end date
返回两者之间的每个“类型”(月/日/小时等)的数组,不包括结束日期
Example:
例子:
let range = getRange('2019-01-01', '2019-01-05', 'days')
// moment dates -> [ '2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04' ]