HTML code for time stamp

Asked By 50 points N/A Posted on -
qa-featured

Hello Techyv Friends,

I know HTML a bit but don’t know the HTML code for time stamp, So I request Techyv guys to help me by posting a simple HTML code for the time stamp that will display the current date and time in the webpage [Example : 15/12/2013 01:01:01 AM].

Thanks and Regards,

Marvin M Calderon

SHARE
Best Answer by Lynda M Harvey
Best Answer
Best Answer
Answered By 0 points N/A #167697

HTML code for time stamp

qa-featured

Hi Marvin,

You cannot  create any dynamic web page using HTML because it is used for formatting  web pages.

For displaying the current time stamp you need to use any scripting language like javascript or vbscript.

Vbscript code

<html>

<script type=”text/vbscript”>

Function Displaydatetime()

Frmtimestamp.txtdate.text=FormatDateTime(Date, 0);

Frmtimestamp.txtdate.text= Frmtimestamp.txtdate.text & FormatDateTime(Time,3);

End Function

</script>

<body onload=”Displaydatetime()”>

<form name=”frmtimestamp”>

<input type=”text” name=”txtdatetime”>

</form>

</body>

</html>

Syntax:

FormatDateTime(dateOrTime, format)

The dateOrTime parameter is the date or time you want to format, and the format parameter is the type of formatting to apply.The format parameter can take a value from 0 – 4, each value signifying a different type of formatting:

0 (used for dates) – display a date in the format mm/dd/yy (default)

1 (used for dates) – display a date in the format day, month and month day, year

2 (used for dates) – display a date in the format mm/dd/yy (default (same as 0))

3 (used for time) – display time in the format hh:mm:ss: PM/AM (12 hour format)

4 (used for time) – display time in the format hh:mm (24 hour format)

 

 

Javascript Code

<html>

<script type=”text/javascript”>

function Displaydatetime()

{

var tsDate=new Date();

var date=ts.getDate();

var month=ts.getMonth()+1;

var year=ts.getFullYear();

var hours=ts.getHours();

var minutes=ts.getMinutes();

var seconds=ts.getSeconds();

var suffix = "AM";

                if (hours >= 12) {

                suffix = "PM";

                hours = hours – 12;

                }

                if (hours == 0) {

                hours = 12;

                }

Frmtimestamp.txtdate.text= date + "/" + month + "/" + year ;

Frmtimestamp.txtdate.text= Frmtimestamp.txtdate.text + hours + " : " + minutes + " : " + seconds + suffix;

 

}

</script>

<body onload=”Displaydatetime()”>

<form name=”frmtimestamp”>

<input type=”text” name=”txtdatetime”>

</form>

</body>

</html>

In javascript whenever you write new Date() it shows the current date in standard format    with Current day,date,time and GMT.You can have your own format.

  eg:suppose today is 12-05-2012 then

getDate() shows current date(eg: 12)

              getMonth() shows current month (eg: 5)

              getFullYear() shows current year as 2012.

              getHours() shows current hours in 24 hrs format.

              getMinutes()  shows current minutes.

              getSeconds()  shows current seconds

             After getting date ,month ,year and time you can print these according to desired format.

 

Answered By 10 points N/A #167695

HTML code for time stamp

qa-featured

 

 

 

It means your desirable output is:

 

Before click on that button     

 

You should try this one

<SCRIPT LANGUAGE="JavaScript">

function TIMESTAMP()

{

   var Stamp = new Date()

   var Hours;

   var Mins;

   var Time;

   Hours = Stamp.getHours();

   if (Hours >= 12) {

      Time = " P.M.";

   }

   else {

      Time = " A.M.";

   }

   if (Hours > 12) {

      Hours -= 12;

   }

   if (Hours == 0) {

      Hours = 12;

   }

   Mins = Stamp.getMinutes();

   if (Mins < 10) {

      Mins = "0" + Mins;

   }

   str = ''

   str += (Stamp.getMonth() < 10 ? "0" + (Stamp.getMonth() + 1) : Stamp.getMonth() + 1 )

         + "/" + (Stamp.getDate() < 10 ? "0" + Stamp.getDate() : Stamp.getDate() )

         +  "/" + Stamp.getYear() + " "

   str +=Hours + ":"

   str +=Mins + ":"

   str += Time + " "

  

 

   document.getElementById('myDiv').innerHTML = str

 

}

</script>

<table width="400" border="1" cellspacing="0" cellpadding="2">

<tr>

<td width="100">

<div align="center">

<input type="button" name="Button" value="See the time"

 

onclick="TIMESTAMP()" />

</div></td>

<td><div id="myDiv">&nbsp;</div></td>

</tr>

</table>

Cope and paste to your site anywhere as your want.

 

 

 

Login/Register to Answer

Related Questions