I am trying to implement a validation, the validation is basically for covid doses between two dates. so for example, if a person takes a job say today he/he she wont be allowed to take another dose for the next 3 months, basically any month or date selected in between will throw and error. I tried using moment inBetween fucntion, but somehow, it is throwing incorrect result. this is what i am trying to do.
if (moment(18-10-2022, 'DD-MM-YYYY').isBetween(18-01-2023, 'DD-MM-YYYY'))) {
setIsDateError({error: true,message: 'Date of administration of First dose cannot be after the Date of administration of Second Dose in Covid Vaccination Tab'})
return;
}
so validation will trigger as the date lies in between. any help will be appericated.
CodePudding user response:
The call to isBetween is invalid in your implementation. isBetween takes 4 arguments.
- Start date - You can pass a string or a moment object.
- End date - You can pass a string or a moment object.
- unit :- 'year', 'month', 'week' and few more.
- inclusivity parameters - '()','[]','(]','[)'
Please refer :- https://momentjscom.readthedocs.io/en/latest/moment/05-query/06-is-between/ for more details.
CodePudding user response:
Check the below code. It may be helpful for your problem.
var startDate = moment("15/02/2022", "DD/MM/YYYY");
var endDate = moment("20/02/2022", "DD/MM/YYYY");
var testDate = moment("15/02/2022", "DD/MM/YYYY");
testDate.isBetween(startDate, endDate, 'days', true); // will return true
testDate.isBetween(startDate, endDate, 'days', false); // will return false
CodePudding user response:
isBetween
takes at least the start and the end of the date range.
This snippet for example works:
console.log(moment('2022-10-30').isBetween('2022-10-17', '2023-01-17'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>