Latest tutorial: Premium Flash Files | Ask Tutorial5! | Subscribe to RSS Register Login Find Hobbies
Advertisement

Get tutorials on EMail




Do you need more help? You can now Ask Tutorial5! and get free support - Ask a question now!

Display Date with JavaScript

(19 votes)
Written by AnaS   

In this tutorial it will be explained how to display the Date in your website using JavaScript. The Date will be shown in the format Sunday, May 20 2007. Put the following code in the head section of your HTML document:

<script language="javascript" type="text/javascript">
var d=datetime.getDate()

var datetime=new Date()
var day=datetime.getDay()
var month=datetime.getMonth()
var year=datetime.getYear()
if (year < 1000)
year+=1900
var days=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var months=new Array("January","February","March","April","May","June","July","August","September","Octomber","November","December")
</script>


First we create the datetime variable that is a Date() type object. Using the methods available we create the d (the day of the month – a number between 0 and 31), day (the day of the week – a number between 0 and 6, first day is Sunday), month (a number between 0 and 11) and year.


Then we create two arrays: the days (containing the days of the week by their names) and the monts (containing the months of the year by their names).


Now in the body secton put this code:

<body>
<script language="JavaScript" type="text/javascript">
document.write(days[day]+", "+months[month]+" "+d+" "+year)
</script>
</body>

Display date with JavaScript example



Subscribe now via RSS feed and get all the new tutorials

written by b , October 06, 2007

These two sentences are as clear as mud: 1) "First we create the datetime variable that is a Date() type object." and 2) "Using the methods available..."

What would be most helpful would be to see a completed sample version of what you're trying to describe.
written by Tamil Maran , April 16, 2008

How to get a online date by using a javascript. But not the system Date.

Do you need more help? Ask now!
 

busy
Last Updated ( Tuesday, 10 July 2007 )