Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Display Date with JavaScript

(28 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.
written by john23323 , September 12, 2008

doesn't work
written by themaster , October 08, 2008

sometimes you have to take out the body and just put this were you wish to show your date


document.write(days[day] ", " months[month] " " d " " year)


then it worked fine for me

written by themaster , October 08, 2008

well tat fucked up cuz of the censor but just take out the at the start and end of your show clock script
written by themaster , October 08, 2008

i meant take out the () at the start and end of your show date script ( the website keeps fukin up my comments
written by werqtr , October 29, 2008

var d=datetime.getDate()

is in the wrong line, you need to initalize date first
So these 2:

var d=datetime.getDate()

var datetime=new Date()

Like this :


var d=datetime.getDate()

var datetime=new Date()
written by muni , March 13, 2009

i want to required date format like this day-mon-year (i.e., 13-03-2009) send javascript code
written by ddon , April 13, 2009







var mydate=new Date()
var year=mydate.getYear()

if (year < 1000)
year =1900

var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()

if (daym
written by rkstudio6 , April 21, 2009

I just want the year - can you post that code? Thanks!
written by kyle steed , April 29, 2009

Not sure why, but I couldn't get your code to work for me. If anyone else has the same problem, try this:



var mydate=new Date()
var year=mydate.getYear()

if (year < 1000)
year =1900

var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()

if (daym
written by dfghg , October 28, 2009

nothing got cleared
written by jkhjkhj , December 20, 2009

hkhjk
written by olivia Dike , June 15, 2010

can u email me the whole java script for designing a web site and how to write a java script...
Thanks
written by bee , September 07, 2010

can u e mail me the code to dispaly system time and date using two buttons in javascript, one to display system time when i click it and the other one for the date. please

Do you need more help? Ask now!
 

busy
Last Updated ( Tuesday, 10 July 2007 )