WITH#. The same code only works with 1 label ... why?

I made 2 radio buttons so that the user can choose whether he wants to watch the time in 24h format or in 12h format, this is the code that I have on the timer:

var format = rad24h.Checked ? "HH:mm" : "hh:mm:ss tt";

        timer1.Interval = 500;

        DateTime myDateTime = DateTime.Now;

        label1.Text = string.Format("Hora actual {0}\n  ", myDateTime.ToString(format));

        lblHK.Text = string.Format("Hong Kong {0}\n  ", myDateTime.AddHours(7).ToString(format));

        lblNY.Text = string.Format("Nova Iorque {0}\n   ", myDateTime.AddHours(-5).ToString(format));

        lblUkr.Text = string.Format("Ucrânia {0}\n", myDateTime.AddHours(2).ToString(format));

        lblTay.Text = string.Format("Taymyrskiy {0}\n  ", myDateTime.AddHours(3).ToString(format));

        lblAla.Text = string.Format("  Alaska {0}\n", myDateTime.AddHours(-9).ToLongString(format));

        lblUru.Text = string.Format("Uruguay {0}\n", myDateTime.AddHours(-4).ToString(format));

        lblSyd.Text = string.Format(" Sydney {0}\n", myDateTime.AddHours(9).ToString(format));

        lblMad.Text = string.Format("Madagascar {0}\n   ", myDateTime.AddHours(2).ToString(format));

The only label that works with this is lblUkr, (fourth from top to bottom) ...

I checked everything on the other lines to make sure they are the same, what am I missing?

Also, the label does not show AM / PM ... how can I do this?

UPDATE

, - ... , , lblUkr, , , 24h , , , ... , - , , , ... , ?

2

, , , , AM, PM , ?

+3
4

. , .

, , Tick...

, " ", :

var format = false ? "HH:mm" : "hh:mm:ss tt";
DateTime myDateTime = DateTime.Now;
Console.WriteLine(string.Format("Hora actual {0}\n  ", myDateTime.ToString(format)));
Console.WriteLine(string.Format("Hong Kong {0}\n  ", myDateTime.AddHours(7).ToString(format)));
Console.WriteLine(string.Format("Nova Iorque {0}\n   ", myDateTime.AddHours(-5).ToString(format)));
Console.WriteLine(string.Format("Ucrânia {0}\n", myDateTime.AddHours(2).ToString(format)));
Console.WriteLine(string.Format("Taymyrskiy {0}\n  ", myDateTime.AddHours(3).ToString(format)));
Console.WriteLine(string.Format("  Alaska {0}\n", myDateTime.AddHours(-9).ToString(format)));
Console.WriteLine(string.Format("Uruguay {0}\n", myDateTime.AddHours(-4).ToString(format)));
Console.WriteLine(string.Format(" Sydney {0}\n", myDateTime.AddHours(9).ToString(format)));
Console.WriteLine(string.Format("Madagascar {0}\n   ", myDateTime.AddHours(2).ToString(format)));

var format = false ... var format = true ..., 12- 24- . , , .

, /, ...

+3
DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01 PM  

.

Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.

- MSDN

, , :)

+2

The code you posted should work. Try calling .Invalidate()or .Refresh()for each of the labels to make sure they are redrawn. Also, I assume that you are using System.Windows.Forms.Timer, so there is no access to end-to-end flow control, right?

+1
source

Check the format, H - 24-hour format, h - 12 hours.

and 'tt' for AM or PM.

0
source

All Articles