I am working on a small C# / ASP.NET project that will display 4 or 5 different labels to show what time it is in different parts of the world.
I have been researching tons of different questions and blog posts about this across the net but I’m still missing a few pieces that a more experienced developer could fill in no problem. I have read up on things like “TimeZoneInfo” and “ToLocalTime” and “GetUTCOffSet” and the like.. and to someone as new as me it is getting confusing. I must say most of this info I have seen right here on Stack Overflow. I’d like to show what I have currently and see if I could get some advice here:
Code behind:
protected void Timer1_Tick(object sender, EventArgs e)
{
string militaryTime = ( "HH:mm:ss tt" );
lblTimer1.Text = DateTime.Now.ToString(militaryTime);
}
Mark up on the .aspx page:
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
<h4 style="width: 200px">
Local : <asp:Label ID="lblTimer1" runat="server"></asp:Label>
</h4>
Now, I understand that this is probably going to make more sense to do client side in Javascript, but this task is also a learning tool on how to make this happen in C# without the JS.
Anyway, I’m having trouble figuring out how to display different time zones for the following 4 labels I have. I want it to work off of DateTimeNow because this could be seen anywhere in the world in any time zone, and it should update the other 4 labels representing different world cities based on the users time zone in relation to those cities.
Again, I understand there are a lot of threads out there about this, and as you read this I am researching them, but so far I’m still missing some of the basic structure I need to build from.
Thank you in advance.
Try to use DateTimeOffset
The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance’s date and time and Coordinated Universal Time (UTC).