Date and Time by Location C#
I want to try my hand at building a simple Date Time app that shows the local time of the computer it is on and then add the ability to change the date and time by location via a drop down box.
I should mention that I am a complete beginner.
I have managed 开发者_StackOverflowto accomplish the first part where my app displays the local time in a label. I would like to know how I can get the time and date by a location but not just a country, say specific states in the city e.g Los Angeles in the US or moscow in Russia. I've read about the CultureInfo.DateTimeFormat property but I'm not sure if that will give me what I'm looking for.
Any pointers would be good, I'm not looking for anyone to write the code for me, I'd learn nothing that way, but I would like an example or maybe a nudge in the right direction. Thanks.
Have a look at the TimeZone and TimeZoneInformation in the msdn documentation.
You will need to get the timeoffsets for each location and then add those offsets to the DateTime value. Remember these offsets are with respect to GMT Time which you can get using
.ToUniversalTime()
moscowDt = localDt.ToUniversalTime().AddMinutes(180);
As moscow id GMT +3 hrs
Here is a blog post that should give you the answer you need. Basicaly, you have to save your date in UTC, and apply the + or - hours for your timezone.
精彩评论