Javascript: How to Get the Date, Month, Year and Weekday

Using the Date class
<script type="text/javascript">

hereIsMyDate = new Date();

dayOfTheMonth = hereIsMyDate.getDate(); // RETURNS INTEGER 1-31

monthNumber = hereIsMyDate.getMonth(); // RETRUNS INTEGER 0-11 (for Jan-Dec)

weekdayNumber = hereIsMyDate.getDay(); // RETURNS INTEGER 0-6 (for Mon-Fri)

fullYear = hereIsMyDate.getFullYear(); // RETURNS 4-DIGIT YEAR (ex. 2010)

monthNamesArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

monthName = monthNamesArray[monthNumber];

dayNamesArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday");

dayOfTheWeekName = dayNamesArray[weekdayNumber];

document.write("<p>Today's date is " + dayOfTheWeekName + ", " + monthName + ", " + dayOfTheMonth + ", " + fullYear + "</p>");

document.write("<p>&copy; " + fullYear + " MyWebsiteName</p>");

Javascript has a set of functions built into the language that will give you information about dates.

The methods used to work with dates have been bundled together in something called a class. The class you will be using must be referred to by its pre-defined name. Conveniently for us, it is called: Date()

The first thing to do when using the Date class is to create something called an object. An object allows you to make a working version of the class. (You can't work with the Date() class directly.) You need to pick a name for this new object.

Here we'll call it: hereIsMyDate

To create this object (this copy of the Date() class), you write a statement like this:
hereIsMyDate = new Date();
Now, you can start using the functions or methods that come from the Date() class. There are many functions available including dates, times, and the ability to change dates and times. These functions should make for easy ways to go forward or backward in time and get information about that date. You could, for example, figure out what day of the week Valentine's Day will occur next year, what the time and date will be 8 hours from now, or perhaps get data about a date that occurred exactly 1035 days ago.

To get the current day of the month you use the .getFullYear() method: 
fullYear = hereIsMyDate.getFullYear();
To get the current day of the month you use the .getDate() method:
dayOfTheMonth = hereIsMyDate.getDate();
To get the  current day of the week, you use the .getDay() method. However, you will not get a string like "Monday" or "Mon," you will only get back an integer from 0 to 6, which represent Sunday to Saturday. To get a string you would have to create a separate array of date names and use the value sent back from the .getDay method to show a text version of the date:
dayOfTheWeekNumber = hereIsMyDate.getDay();

dayNamesArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

dayOfTheWeekName = dayNamesArray[dayOfTheWeekNumber];

Similarly, to get the current month, you would use the .getMonth() method. The value returned will be from 0 to 11, representing January to December. If necessary, create an array of month names:
monthNumber = hereIsMyDate.getMonth();

monthNamesArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

monthName = monthNamesArray[monthNumber];

Once you have your parts assigned to variables, you can create different uses for them. For example the code at the top of this document will output the following:

Today's date is Thursday, September, 24, 2009

© 2009 MyWebsiteName

For the most technical explanation of the Date() class and the way it works, please see Section 5.9 of the official JavaScript (a/k/a ECMAScript) specification ECMA-262.

No comments :

Post a Comment