Click here to Skip to main content
15,914,289 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a form that I have a code behind for the time. Is there a way to have the time to move like a clock? Ex: If it is 12:30pm I sound see it turn to 12:31pm in a minute not refresh the page for the time to change.

Here is my time code:

C#
this.lblTime.Text = System.DateTime.Now.ToShortTimeString();


I also tried this:

C#
this.lblTime.Text = DateTime.Now.ToString("HH:mm:ss ");
 and
this.lblTime.Text = Strings.Format("The current time is {0:HH:mm:ss}.", DateTime.Now);


No of these the time changed without refreshing the page.
Posted

1 solution

Of you are talking about a page, then forget the word "form" - it has a specific meaning, and requires a different solution.

For a web page, the best way to do this is via JavaScript so it is kept to the client, and doesn't need a round trip to the server every second...

See here: http://www.w3schools.com/js/js_timing.asp[^] - it presents several ways to display the time!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900