简体   繁体   English

在 nodejs moment.js 中按月进行日期范围验证

[英]date range validation by month in nodejs moment.js

I am trying to validate or check the date range by passing month and year, If date range come under the given month range between, then it would be valid else error.我试图通过传递月份和年份来验证或检查日期范围,如果日期范围在给定的月份范围内,那么它将是有效的,否则会出错。

month = "04, 06, 2022"; // from-month, to-month, year

date = "12-05-2022" // DD-MM-YYYY
from month = 04
to month = 06
year = 2022

I read the documentation but didn't achive the goal.我阅读了文档但没有达到目标。 I want if I am passing this data below -我想如果我在下面传递这些数据 -

month = "04, 06, 2022"; // from-month, to-month, year
date = "12-07-2022" // DD-MM-YYYY => error
date = "12-05-2022" // DD-MM-YYYY => success
let value = '12-05-2022'; //DD-MM-YYYY
let check = moment(value,'DD-MM-YYYY', true).isValid();
console.log(check) //returns true
return check

Try to use a range to check if the date corresponding to value is contained:尝试使用range检查是否包含value对应的日期:

const year = 2022;
const fromMonth = 4;
const toMonth = 6;

const startDate = new Date(year, fromMonth, 12);
const endDate = new Date(year, toMonth, 15);

const value = '12-05-2022';
const check = moment().range(startDate, endDate).contains(new Date(value));

console.log(check);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM