Javascript 如何使用矩获取相同日期的数据?
我下面的方法是,如果日期介于两者之间,则返回true,但如果日期等于开始/结束日期,则我也希望返回true,是否可以使用“时刻”来执行此操作 梅因酒店Javascript 如何使用矩获取相同日期的数据?,javascript,typescript,momentjs,Javascript,Typescript,Momentjs,我下面的方法是,如果日期介于两者之间,则返回true,但如果日期等于开始/结束日期,则我也希望返回true,是否可以使用“时刻”来执行此操作 梅因酒店 function checkDateRange(startDate: string, endDate: string, fillDate: string): boolean { if (fillDate > startDate && fillDate < endDate) { const _fi
function checkDateRange(startDate: string, endDate: string, fillDate: string): boolean {
if (fillDate > startDate && fillDate < endDate) {
const _fillDate = moment(fillDate).utc();
return _fillDate.isBetween(startDate, endDate);
}
return false;
}
函数checkDateRange(startDate:string,endDate:string,fillDate:string):布尔值{
如果(fillDate>startDate&&fillDate
您可以将包容性参数添加到:
版本2.13.0引入了包容性。A[
表示包含值。A(
表示排除。
如果使用包容性参数,则必须通过两个指标
如果未指定包容性参数,力矩将默认为()
您的代码可以如下所示:
function checkDateRange(startDate: string, endDate: string, fillDate: string): boolean {
if (fillDate > startDate && fillDate < endDate) {
const _fillDate = moment(fillDate).utc();
return _fillDate.isBetween(startDate, endDate, null, '[]');
}
return false;
}
函数checkDateRange(startDate:string,endDate:string,fillDate:string):布尔值{
如果(fillDate>startDate&&fillDate
由于函数返回布尔值,因此只需返回条件测试的值。!before和!after与before和before相同,包括
function checkDateRange(startDateStr: string, endDateStr: string, fillDateStr: string): boolean {
let startDate = moment(startDateStr);
let endDate = moment(endDateStr);
let fillDate = moment(fillDateStr);
return !fillDate.isBefore(startDate) && !fillDate.isAfter(endDate)
}
第一步不应该是将字符串参数解析为矩日期吗?之后,矩提供了一些处理相等和不相等的方法,包括diff-ing、isBefore、isame、isAfter