News: Write the current date using JavaScript

Home News Photos News Videos News Cartoons News Blogs RSS

Home > Java Scripts Examples and Tutorials > Write the current date using JavaScript

Write the current date using JavaScript

Using the date() object in JavaScript, we can print the current date in a variety of popular formats.

Wednesday, Apr 23, 2008 | 1723 Views | Comments [View/Post]

The JavaScript date() object

In JavaScript, the date() object stores the current date in raw form. We can write the date in its raw form by using the following code:

<script type="text/javascript">document.write(date());</script>

The output looks like this:

Sun Nov 27 14:10:51 EST 2005

As shown in this example, the output of the date() object is not very "clean." In most cases, we would want to output the date in a more user-friendly format. To achieve this, we initialize the date() object (as done above), then modify the output as needed using built-in methods of the date() object.

Write the day using JavaScript

To write the day using JavaScript, we need to initialize the date() object, use the getDay method to return a numerical value corresponding to the current day, then output the day in whatever format we choose. This is actually much simpler than it sounds.

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)

// Make day number value correspond to actual day name
var dayName=new Array(7)
dayName[0]="Sunday";
dayName[1]="Monday";
dayName[2]="Tuesday";
dayName[3]="Wednesday";
dayName[4]="Thursday";
dayName[5]="Friday";
dayName[6]="Saturday";

document.write (dayName[day]); // Write the day
</script>

Write the date using JavaScript

We might also need to write the date. This is accomplished by initializing the date() object, getting the current date in numerical form by using the getDate method, than outputting the date in whatever format we choose. In this example, I want to write the date in number form, but I also want to append the appropriate suffix corresponding to the date (i.e. "th", "st", "nd", etc.).

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
date=today.getDate(); // Get the numerical date

// Add suffix to date (1st, 2nd, 4th, etc.)
if (date==1) suffix=("st");
else if (date==2) suffix=("nd");
else if (date==3) suffix=("rd");
else if (date==21) suffix=("st");
else if (date==22) suffix=("nd");
else if (date==23) suffix=("rd");
else if (date==31) suffix=("st");
else suffix=("th");

document.write (date + suffix); //Write the date
</script>

Write the month using JavaScript

We’re only missing two components of a complete date: the month and year.

To write the month, we need to initialize the date() object, get the current month in raw form (0,1,2,3,4,etc.) by using the getMonth method, then output the date in whatever form we like.

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
month=today.getMonth()+1; // Get the month

// Make month number correspond to month name
if (month==1) monthName=("January");
else if (month==2) monthName=("February");
else if (month==3) monthName=("March");
else if (month==4) monthName=("April");
else if (month==5) monthName=("May");
else if (month==6) monthName=("June");
else if (month==7) monthName=("July");
else if (month==8) monthName=("August");
else if (month==9) monthName=("September");
else if (month==10) monthName=("October");
else if (month==11) monthName=("November");
else monthName=("December");

document.write (monthName); //Write the month
</script>

Write the year using Javascript

To write the current year, we need to initialize the date() object, then use the getYear method to return the year. This is the easiest part of writing the date in JavaScript.

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
year=today.getYear(); // Get the year

document.write (year);
</script>

Write the full date using JavaScript

So far, we’ve learned to get the day, date, month, and year in JavaScript. To write the full date, all we need to do is combine the examples above. The following code will output the date in the form of "Sunday, November 27th, 2005":

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
date=today.getDate(); // Get the numerical date
year=today.getYear(); // Get the year
day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)
month=today.getMonth()+1; // Get the month

// Make day number value correspond to actual day name
var dayName=new Array(7)
dayName[0]="Sunday";
dayName[1]="Monday";
dayName[2]="Tuesday";
dayName[3]="Wednesday";
dayName[4]="Thursday";
dayName[5]="Friday";
dayName[6]="Saturday";

// Add suffix to date (1st, 2nd, 4th, etc.)
if (date==1) suffix=("st");
else if (date==2) suffix=("nd");
else if (date==3) suffix=("rd");
else if (date==21) suffix=("st");
else if (date==22) suffix=("nd");
else if (date==23) suffix=("rd");
else if (date==31) suffix=("st");
else suffix=("th");

// Make month number correspond to month name
if (month==1) monthName=("January");
else if (month==2) monthName=("February");
else if (month==3) monthName=("March");
else if (month==4) monthName=("April");
else if (month==5) monthName=("May");
else if (month==6) monthName=("June");
else if (month==7) monthName=("July");
else if (month==8) monthName=("August");
else if (month==9) monthName=("September");
else if (month==10) monthName=("October");
else if (month==11) monthName=("November");
else monthName=("December");

// Write date
document.write(dayName[day] + ", " + monthName + " " + date + suffix + ", " + year);
</script>


Comment on this article
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
News Other Language
World News
Movie News
Information
Travel News