Forum Discussion
RobbSalzmann
2 years agoValued Contributor II
The following will give you a date time group with the three letter Time Zone designation:
25/08/2023 5:00:00PM CDT
Dim currentDateTime As DateTime = DateTime.Now
Dim formattedDateTime As String = currentDateTime.ToString("dd/MM/yyyy h:mm:ss tt")
Dim regex As New Regex("\b\w")
Dim tz As String = String.Concat(regex.Matches(TimeZoneInfo.Local.StandardName).Cast(Of Match)().Select(Function(m) m.Value))
return $"{formattedDateTime} {tz}"
Note: timezone information returned is based on the runtime environment of the OS server.
If you store your User's time zone ids in a table, you can use the following to get the correct date and time for their TZ:
Dim targetTimeZoneId As String = "Central Standard Time" ' Replace with the desired timezone ID, e.g., "Central Standard Time" Dim targetTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(targetTimeZoneId) Dim currentDateTime As DateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, targetTimeZone) Dim formattedDateTime As String = currentDateTime.ToString("dd/MM/yyyy h:mm:ss tt") Dim regex As New Regex("\b\w") Dim tz As String = String.Concat(regex.Matches(targetTimeZoneId).Cast(Of Match)().Select(Function(m) m.Value)) return $"{formattedDateTime } {tz}"
Related Content
- 5 months ago
- 3 years ago
- 3 years ago