October 21, 1999 - What Day were you Born on? | WebReference

October 21, 1999 - What Day were you Born on?

Yehuda Shiran October 21, 1999
What Day were you Born on?
Tips: October 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

The Date object is one of the more powerful objects in JavaScript. It supports several methods to set and get different portions of the date description. Let's take an example that computes the day of the week of your birthday:

var ar = new Array();
ar[0] = "Sunday";
ar[1] = "Monday";
ar[2] = "Tuesday";
ar[3] = "Wednesday";
ar[4] = "Thursday";
ar[5] = "Friday";
ar[6] = "Saturday";
var birthDay = new Date("January 3, 1978");
var day = birthDay.getDay();
alert("You were born on " + ar[day]);

Notice that Sunday is the first day of the week and its index is 0. Similarly, the first month of the year is January and its numeric value is 0 as well. The following script sets the year, month, and date of the month:

var ar = new Array();
ar[0] = "Sunday";
ar[1] = "Monday";
ar[2] = "Tuesday";
ar[3] = "Wednesday";
ar[4] = "Thursday";
ar[5] = "Friday";
ar[6] = "Saturday";
var birthDay = new Date();
birthDay.setYear(1978);
birthDay.setMonth(0);
birthDay.setDate(3);
var day = birthDay.getDay();
alert("You were born on " + ar[day]);