Java Script program to display current date and time using HTML.

 Date and time are the important factors for real time applications. JavaScript includes inbuilt class to get the date and time.

date() is the inbuilt class. 

This inbuilt class has many functions listed below.

 getDate() - it provides the date as number(1-31)

 getMonth() - it displays the month as number

getFullYear() - it gives the year in four digit number

getDay() - it shows the day of the week in number(0-6)

getHour() - it displays the hour information(0-23)

getMinutes()- it gives the minutes details( 0-59)

getSeconds() -it provides  the seconds details (0-59)

getMilliseconds() -it finds the milliseconds details and gives the information( 0-999)

getTime()- it shows the time.

these are the inbuilt functions available in date().


Next, create a file in notepad ,save it as "DateDisplay.html" .

The program is given below.

The <head> tag  is used to set the heading as "Display current date and time" with <h1> tag.

where <head> tag is for heading

<h1> tag is for heading style1 with  red color 

<body bgcolor="yellow"> - it sets the background color as yellow.


"Datedisplay.html"

<html> 

<head> <h1 style="color:red;"> Display current date and time </head>

<body bgcolor="Yellow"

<br>

< !-- This set the heading style as 2 with text color as blue -->

<h2 style="color:blue;"> <marquee> Current date and time is:</marquee>

<br>

<!-- Scripting code  -->

<script> function display()

{

 var a="You have clicked"; 

<!-- creating object for Date() -->

var date=new Date(); 

<!-- using date.getData() ,date1 retrieve the value of current date -->

var date1=date.getDate();

<!--  using date.getMonth(),month1 is assigned with current month value-->

 var month1=date.getMonth();

<!-- month value is always 0 to 11 as default. so we increase one by getting exact month --> 

 month1++

<!-- year1 is assigned the current year value -->

var year1=date.getFullYear(); 

<!-- This executes when the button is presses -->

document.getElementById("dis").value=date+"/"+month1+"/"+year1;

 } 

</script> 

<form> 

<!-- This creates a text box and a button -->

<input type="text" id="dis" /><br /> 

<input type="button" value="Display Date" onclick="display()" /> 

</form> 

<body> </html> 

Once you completed it, just open a browser and type the URL of the file and click it.

It shows the output.


This is a simple html program with java script to display current date and time.  Try do it some more programs using the above inbuilt functions.





 

No comments:

Post a Comment