You can use a custom format string to format the time:
Dim formatted As String = ConvertedTime.ToString("hh':'mm':'ss tt")
Reference: Custom Date and Time Format Strings
Domanda
I am making an app which shows times of various countries in the world. The user can select the Timezone from the combobox and the time should be displayed in the Label. I'm using the following code to convert UTC time to selected timezone:
Dim ConvertedTime As Date = TimeZoneInfo.ConvertTimeFromUtc(Date.UtcNow, CBTime1.SelectedItem)
CBTime1 lists all the timezones by the code:
Dim TimeZones As ReadOnlyCollection(Of TimeZoneInfo)
TimeZones = TimeZoneInfo.GetSystemTimeZones()
CBTime1.BindingContext = New BindingContext
CBTime1.DataSource = TimeZones
What should be the code to display the converted time in this format: 08:36:27 PM I tried various codes but none worked.
Soluzione
You can use a custom format string to format the time:
Dim formatted As String = ConvertedTime.ToString("hh':'mm':'ss tt")
Reference: Custom Date and Time Format Strings