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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

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]);


People who read this tip also read these tips:

Look for similar tips by subject:

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji