为什么 Moment isBetween 不起作用?结果是假的
Why Moment isBetween not working? result is false
为什么 Moment isBetween 不起作用? moment(obj.date).isBetween
返回错误。
let dates = [
{date:'11/26/2020'},
{date:'11/26/2020'},
{date:'11/27/2020'},
];
let filtered_data = _.filter(dates, (obj)=>{
console.log(moment(moment(obj.date).format('YYYY-MM-DD')).isBetween('2020-11-25', '2020-11-26'));
console.log(moment(obj.date).format('YYYY-MM-DD'));
return moment(obj.date).isBetween('2020-11-25', '2020-11-25');
});
console.log(filtered_data);
通过这种方式从字符串 MM-DD-YYYY
中获取你的日期
moment(obj.date, 'MM-DD-YYYY')
var data = [
{date:'11/26/2020'},
{date:'11/27/2020'},
{date:'11/28/2020'}
];
let filtered_data = data.filter(obj => {
let dateToCompare = moment(obj.date, 'MM-DD-YYYY');
return dateToCompare.isBetween('2020-11-25', '2020-11-28');
});
console.log(filtered_data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
Moment JS isBetween 仅在今天之间检查。
例如:-
dates = [{date:'11/26/2020'},{date:'11/26/2020'},{date:'11/27/2020'}];
let filtered_data = dates.filter((obj)=>moment(obj.date).isBetween('2020-11-25',
'2020-11-27'));
输出:-
[{"date":"11/26/2020"},{"date":"11/26/2020"}]
检查这个 momentjs 游乐场示例:-
let filtered_data = _.filter((obj: any) => {
console.log(moment(moment(obj.date).format('YYYY-MM-DD')).isBetween('2020-11-25', '2020-11-26', undefined, '[]'));
console.log(moment(obj.date).format('YYYY-MM-DD'));
return moment(moment(obj.date).format('YYYY-MM-DD')).isBetween('2020-11-25', '2020-11-26', undefined, '[]');
});
这是其他例子
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', undefined, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', undefined, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', undefined, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', undefined, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', undefined, '[]'); //true
参考这个link
为什么 Moment isBetween 不起作用? moment(obj.date).isBetween
返回错误。
let dates = [
{date:'11/26/2020'},
{date:'11/26/2020'},
{date:'11/27/2020'},
];
let filtered_data = _.filter(dates, (obj)=>{
console.log(moment(moment(obj.date).format('YYYY-MM-DD')).isBetween('2020-11-25', '2020-11-26'));
console.log(moment(obj.date).format('YYYY-MM-DD'));
return moment(obj.date).isBetween('2020-11-25', '2020-11-25');
});
console.log(filtered_data);
通过这种方式从字符串 MM-DD-YYYY
中获取你的日期
moment(obj.date, 'MM-DD-YYYY')
var data = [
{date:'11/26/2020'},
{date:'11/27/2020'},
{date:'11/28/2020'}
];
let filtered_data = data.filter(obj => {
let dateToCompare = moment(obj.date, 'MM-DD-YYYY');
return dateToCompare.isBetween('2020-11-25', '2020-11-28');
});
console.log(filtered_data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
Moment JS isBetween 仅在今天之间检查。
例如:-
dates = [{date:'11/26/2020'},{date:'11/26/2020'},{date:'11/27/2020'}];
let filtered_data = dates.filter((obj)=>moment(obj.date).isBetween('2020-11-25',
'2020-11-27'));
输出:-
[{"date":"11/26/2020"},{"date":"11/26/2020"}]
检查这个 momentjs 游乐场示例:-
let filtered_data = _.filter((obj: any) => {
console.log(moment(moment(obj.date).format('YYYY-MM-DD')).isBetween('2020-11-25', '2020-11-26', undefined, '[]'));
console.log(moment(obj.date).format('YYYY-MM-DD'));
return moment(moment(obj.date).format('YYYY-MM-DD')).isBetween('2020-11-25', '2020-11-26', undefined, '[]');
});
这是其他例子
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', undefined, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', undefined, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', undefined, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', undefined, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', undefined, '[]'); //true
参考这个link