C# Display time in textbox control

Asked

Viewed 968 times

0

I need to show only the local time in a control TextBox.

In the following format:

hh:mm:ss

How could I do that in C#?

  • You can use the MaskedTextBox for this purpose.

3 answers

5

A possible way:

12h format

textBox1.Text = DateTime.Now.ToString("hh:mm:ss");

24-hour format

textBox1.Text = DateTime.Now.ToString("HH:mm:ss");

0

You can use a Binding so that the TextBox is constantly updated as time goes on. The most direct way to do it is directly in XAML. It would look like this:

 <TextBox Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='HH:mm:ss ', Mode=OneWay}"/>

-1

An alternative to the @rubStackOverflow response:

DateTime n = DateTime.Now;
TextBox1.Text = String.Format("{0}:{1}:{2}", n.Hour, n.Minute, n.Second);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.