Dynamically changing of date
This Current Date and time, but I want to have time dynamically changeable like system Time:
DateTime t = DateTime.Now;
toolStripStatusLabel.Text = "Current Date:" + " " + t.ToString("MMMM dddd dd, yyyy")+" " +"current Time:" +" " +t.ToString("hh:mm ss t开发者_运维知识库t");
On your winform, add a Timer Control and a Label Control.
In the Form Load Event add the code
yourTimer.Start();
In the Property sheet of the Timer Control, change the Interval Property to 1000.
Add the Timer Tick Event
private void yourTimer_Tick(object sender, EventArgs e)
{
yourLabel.Text = DateTime.Now.ToString("dd MMM yyyy hh:mm:ss");
}
My guess is that you want the ToolStripStatusLabel text to change with the time. For that, you'll need to have a timer callback. Add a Timer to your form and in its Elapsed handler, set the text to the current time, like you are already doing.
Else you can set the time once from the server side and keep changing the tooltip value from Javascript. In this case you don't have to go to the server side again and again :)
Just some more information
In your code:
DateTime t = DateTime.Now;
toolStripStatusLabel.Text = "Current Date:" + " "
+ t.ToString("MMMM dddd dd, yyyy")
+ " " + "current Time:"
+ " " + t.ToString("hh:mm ss tt");
The current date is evaluated only once. That is to say, DateTime t = DateTime.Now stores the current date in t and this value is never updated again.
So even if you use 't' a hundred times, it will always have the value that was assigned to it.
Like astander pointed out, you need to update it every second or so.
A suggestion:
Instead of using "somestring" + "someotherstring" + "yetanotherstring" you should use String.Format instead. For example (based on the code by astander)
private void yourTimer_Tick(object sender, EventArgs e)
{
yourLabel.Text = String.Format("Current Date: {0}",
DateTime.Now.ToString("dd MMM yyyy hh:mm:ss"));
}
精彩评论