Today, I am going to show you how to display date and time of multiple countries in your Windows application, using C#.
Just consider here, I am going to display date and time of three countries, with 24-hour and 12-hour range format.
Here, I have added some label for representing the country names. For date column, I have created separate labels and for timing, I have created separate labels.
Then, I have added the timer control in my form to diplay the current running time. Just double click the timer button for writing the code, as shown below.
- private void timer1_Tick(object sender, EventArgs e) {
- label2.Text = DateTime.Now.ToString("hh:mm:ss tt");
- label10.Text = DateTime.Now.ToString("HH:mm:ss");
- label7.Text = DateTime.Now.ToString("d-MM-yyyy");
- label14.Text = DateTime.Now.ToString("d-MM-yyyy");
- TimeZoneInfo TZI = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
- DateTime UK = TimeZoneInfo.ConvertTime(DateTime.Now, TZI);
- label3.Text = UK.ToString("hh:mm:ss tt ");
- label11.Text = UK.ToString("HH:mm:ss");
- label8.Text = UK.ToString("d-MM-yyyy");
- label16.Text = UK.ToString("d-MM-yyyy");
- TimeZoneInfo TZIUS = TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");
- DateTime US = TimeZoneInfo.ConvertTime(DateTime.Now, TZIUS);
- label5.Text = US.ToString("hh:mm:ss tt");
- label12.Text = US.ToString("HH:mm:ss");
- label9.Text = US.ToString("d-MM-yyyy");
- label18.Text = US.ToString("d-MM-yyyy");
- }
Then, give below code to start the timer on page load.
- Timer1.start();
- Timer1.Intervels = 1000;
From the above code, you can see that I get the
Timing Information from the local system and I convert that information to string and display in the Windows Form.
Date and Time String Formats
For time formats
For 12-Hours, you can use .......... hh:mm:ss tt
For 24-Hours, you can use .......... HH:mm:ss
For Date Formats
d-MM-yyyy ----- 18-04-2017
dd-MMM-yyyy - 18-Mar-2017
And the final output would be like the below screen.