Is it possible for a regular expression to find out if a date is a holiday or not?

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

+4
source share
5 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 } 
+7
source

Use getDay() , which returns an integer from 0 to 6, where 0 is Sunday and 6 is Saturday.

If the value is 1-5, then this is the day of the week.

0 or 6 means it is a weekend.

+4
source

Javascript has a date class. See http://www.w3schools.com/jsref/jsref_obj_date.asp

What you need to do is create a Date object:

 date = new Date(2010, 5, 19); //Year, month, day 

Note that the month is indexed by zero, so we subtract the value 1. This is June

Then get the day:

 day = date.getDay(); //Day is also 0 indexed. var weekday=new Array(7); weekday[0]="Sunday"; weekday[1]="Monday"; weekday[2]="Tuesday"; weekday[3]="Wednesday"; weekday[4]="Thursday"; weekday[5]="Friday"; weekday[6]="Saturday"; document.write("Today is " + weekday[date.getDay()]); 
+3
source

The Javascript Date.parse function is specified only for the standard IETF time used (for example, "Aug 9, 1995" ), so it is not suitable for your requirement. For 21/04/2010 do you need to break it yourself and use the Date datejs constructor ?

+1
source

The simple answer is NO . Use Date.getDay() as it is created just for this purpose.

0
source

All Articles