Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for a regex to know whether a date is a weekend or not?

Using javascript I need to validate a form field containing a date in the format: 21/04/2010. The date must be a weekday. Is it possible to create a regular expression for this or is there another, better way to do it?

like image 481
Danny Nimmo Avatar asked Dec 08 '22 02:12

Danny Nimmo


1 Answers

Regex is clearly the wrong tool. Use Date.getDay():

var d = new Date();
var parts = dateStr.split("/");
// Date of month is 0-indexed.
var d = new Date(parts[2], parts[1] - 1, parts[0]);
var day = d.getDay();
if(day == 0 || day == 6)
{
  // weekend
}
like image 84
Matthew Flaschen Avatar answered Dec 28 '22 23:12

Matthew Flaschen