Wednesday, May 29, 2019

Get a DateTime set in a particular time zone in C#!

DateTime howSoonIsNow = TimeZoneInfo.ConvertTime(DateTime.Now,
      TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));

 
 

How do you know what to use for the "Central Standard Time" magic string you ask? Great question. You may dig up a list of time zones like this:

ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
foreach (TimeZoneInfo zone in zones)
{
   var magicString = zone.Id;

 
 

Each TimeZoneInfo has a .Id hanging off of it which holds the magic string you need. There are 139 possibilities:

  1. Dateline Standard Time
  2. UTC-11
  3. Aleutian Standard Time
  4. Hawaiian Standard Time
  5. Marquesas Standard Time
  6. Alaskan Standard Time
  7. UTC-09
  8. Pacific Standard Time (Mexico)
  9. UTC-08
  10. Pacific Standard Time
  11. US Mountain Standard Time
  12. Mountain Standard Time (Mexico)
  13. Mountain Standard Time
  14. Central America Standard Time
  15. Central Standard Time
  16. Easter Island Standard Time
  17. Central Standard Time (Mexico)
  18. Canada Central Standard Time
  19. SA Pacific Standard Time
  20. Eastern Standard Time (Mexico)
  21. Eastern Standard Time
  22. Haiti Standard Time
  23. Cuba Standard Time
  24. US Eastern Standard Time
  25. Turks And Caicos Standard Time
  26. Paraguay Standard Time
  27. Atlantic Standard Time
  28. Venezuela Standard Time
  29. Central Brazilian Standard Time
  30. SA Western Standard Time
  31. Pacific SA Standard Time
  32. Newfoundland Standard Time
  33. Tocantins Standard Time
  34. E. South America Standard Time
  35. SA Eastern Standard Time
  36. Argentina Standard Time
  37. Greenland Standard Time
  38. Montevideo Standard Time
  39. Magallanes Standard Time
  40. Saint Pierre Standard Time
  41. Bahia Standard Time
  42. UTC-02
  43. Mid-Atlantic Standard Time
  44. Azores Standard Time
  45. Cape Verde Standard Time
  46. UTC
  47. GMT Standard Time
  48. Greenwich Standard Time
  49. Sao Tome Standard Time
  50. W. Europe Standard Time
  51. Central Europe Standard Time
  52. Romance Standard Time
  53. Morocco Standard Time
  54. Central European Standard Time
  55. W. Central Africa Standard Time
  56. Jordan Standard Time
  57. GTB Standard Time
  58. Middle East Standard Time
  59. Egypt Standard Time
  60. E. Europe Standard Time
  61. Syria Standard Time
  62. West Bank Standard Time
  63. South Africa Standard Time
  64. FLE Standard Time
  65. Israel Standard Time
  66. Kaliningrad Standard Time
  67. Sudan Standard Time
  68. Libya Standard Time
  69. Namibia Standard Time
  70. Arabic Standard Time
  71. Turkey Standard Time
  72. Arab Standard Time
  73. Belarus Standard Time
  74. Russian Standard Time
  75. E. Africa Standard Time
  76. Iran Standard Time
  77. Arabian Standard Time
  78. Astrakhan Standard Time
  79. Azerbaijan Standard Time
  80. Russia Time Zone 3
  81. Mauritius Standard Time
  82. Saratov Standard Time
  83. Georgian Standard Time
  84. Volgograd Standard Time
  85. Caucasus Standard Time
  86. Afghanistan Standard Time
  87. West Asia Standard Time
  88. Ekaterinburg Standard Time
  89. Pakistan Standard Time
  90. Qyzylorda Standard Time
  91. India Standard Time
  92. Sri Lanka Standard Time
  93. Nepal Standard Time
  94. Central Asia Standard Time
  95. Bangladesh Standard Time
  96. Omsk Standard Time
  97. Myanmar Standard Time
  98. SE Asia Standard Time
  99. Altai Standard Time
  100. W. Mongolia Standard Time
  101. North Asia Standard Time
  102. N. Central Asia Standard Time
  103. Tomsk Standard Time
  104. China Standard Time
  105. North Asia East Standard Time
  106. Singapore Standard Time
  107. W. Australia Standard Time
  108. Taipei Standard Time
  109. Ulaanbaatar Standard Time
  110. Aus Central W. Standard Time
  111. Transbaikal Standard Time
  112. Tokyo Standard Time
  113. North Korea Standard Time
  114. Korea Standard Time
  115. Yakutsk Standard Time
  116. Cen. Australia Standard Time
  117. AUS Central Standard Time
  118. E. Australia Standard Time
  119. AUS Eastern Standard Time
  120. West Pacific Standard Time
  121. Tasmania Standard Time
  122. Vladivostok Standard Time
  123. Lord Howe Standard Time
  124. Bougainville Standard Time
  125. Russia Time Zone 10
  126. Magadan Standard Time
  127. Norfolk Standard Time
  128. Sakhalin Standard Time
  129. Central Pacific Standard Time
  130. Russia Time Zone 11
  131. New Zealand Standard Time
  132. UTC+12
  133. Fiji Standard Time
  134. Kamchatka Standard Time
  135. Chatham Islands Standard Time
  136. UTC+13
  137. Tonga Standard Time
  138. Samoa Standard Time
  139. Line Islands Standard Time

No comments:

Post a Comment