从日期中获取时间 VB.Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22768672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Time From Date VB.Net
提问by Leeuwenhok
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:
我正在制作一个显示世界各国时间的应用程序。用户可以从组合框中选择时区,时间应显示在标签中。我正在使用以下代码将 UTC 时间转换为选定的时区:
Dim ConvertedTime As Date = TimeZoneInfo.ConvertTimeFromUtc(Date.UtcNow, CBTime1.SelectedItem)
CBTime1 lists all the timezones by the code:
CBTime1 通过代码列出所有时区:
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.
以这种格式显示转换时间的代码应该是什么: 08:36:27 PM 我尝试了各种代码,但都没有奏效。
采纳答案by Guffa
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

