.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to show digital clock in asp.net

Posted By: priya     Posted Date: January 06, 2010    Points:2   Category :ASP.Net
HI,

How to show digital clock in asp.net? I need to show the current time to the user. I am using C# and asp.net.

I dont want to use Timer control because it will post back the page every second. so i feel javascript based time wold be good. What you suggest?

Thanks
Priya

Responses
Author: Lalij Mer             
Posted Date: January 06, 2010     Points: 5   

try this code sample using javascript

<html>

<head><title>Digital Clock</title>

<style>

.styling

{

background-color:"#ffffcc";

color:navy;

font: 20px MS Sans Serif;

padding: 6px;

}

</style>

</head>

<body bgcolor="#ccccff">Digital Clock: <br /><br />

<span id="digitalclock" class="styling"></span>

<script>

var alternate=0

var standardbrowser=!document.all&&!document.getElementById



if (standardbrowser)

document.write('<form name="form1"><input type="text" name="text1" size="11"></form>')



function show()

{

if (!standardbrowser)

var clockobj=document.getElementById? document.getElementById("digitalclock") : document.all.digitalclock

var Digital=new Date()

var hours=Digital.getHours()

var minutes=Digital.getMinutes()

var dn="AM"



if (hours==12) dn="PM"

if (hours>12)

{

dn="PM"

hours=hours-12

}

if (hours==0) hours=12

if (hours.toString().length==1)

hours="0"+hours

if (minutes<=9)

minutes="0"+minutes



if (standardbrowser)

{

if (alternate==0)

document.form1.text1.value=hours+" : "+minutes+" "+dn

else

document.form1.text1.value=hours+" "+minutes+" "+dn

}

else

{

if (alternate==0)

clockobj.innerHTML=hours+"<font color='#ff0033'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"

else

clockobj.innerHTML=hours+"<font color='blue'>&nbsp;:&nbsp;</font>"+minutes+" "+"<sup style='font-size:2px'>"+dn+"</sup>"

}

alternate=(alternate==0)? 1 : 0

setTimeout("show()",1000)

}

window.onload=show()

</script>

</body>

</html>

Another example here
-------------------

This Code will Create a Digital Clock
1.Create a Form. Here(Abbas is the Form Name)
2.Create Three TextBox for Hours,Minutes, and Seconds Respectively
3.Name the Three TextBox. Here(hours,minutes and seconds are the TextBox Name)


<SCRIPT>
function Abbas()
{
myDate= new Date()
abbas.hours.value=myDate.getHours()
abbas.minutes.value=myDate.getMinutes()
abbas.seconds.value=myDate.getSeconds()
}
window.setInterval("Abbas()",1000)
</SCRIPT>

more example
----------
http://www.xfunda.com/index.php?view=article&id=62%3Ajavascript-digital-clock&option=com_content&Itemid=75

Please Check my answer if you help....
Thank You...

Lalit.


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend