C # Display time in textbox control

0

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

In the following format:

hh:mm:ss

How could I do this in C #?

    
asked by anonymous 20.02.2016 / 15:53

3 answers

5

One possible way:

12h format

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

24h format

textBox1.Text = DateTime.Now.ToString("HH:mm:ss");
    
20.02.2016 / 16:02
0

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

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

An alternative to the @rubStackOverflow response:

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