Joi: Validate date to be greater than x days from now

Viewed 4094

Validate the date field to be greater than x days from now.

Right now I have this snippet that checks if the date is greater than now.

planned_date: Joi.date().greater('now').required()

But I want to validate that the planned_date is at least 2 days more than now. This could be possible combining with moment.js, but couldn't get it working.

1 Answers

This will check for date greater than 2 days, what we are doing is using Date.now() to get current time stamp in epoch later adding 2 days time in epoch to get the validation we wanted.

planned_date: Joi.date().required().greater(Date.now() + 48 * 60 * 60 * 1000)

Related