我想知道如何根据组合框列表中选定的UTC时区在标签中显示时间。提前致谢。到目前为止这是什么。
Private Sub frmClock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label2.Text = TimeOfDay
Dim tzCollection As ReadOnlyCollection(Of TimeZoneInfo)
tzCollection = TimeZoneInfo.GetSystemTimeZones()
Me.ComboBox1.DataSource = tzCollection
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedTimeZone As TimeZoneInfo = DirectCast(Me.ComboBox1.SelectedValue(), TimeZoneInfo)
MsgBox("You selected the " & selectedTimeZone.ToString() & " time zone.")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim dt As Date = TimeOfDay
Label2.Text = dt.ToString()
End Sub
答案 0 :(得分:2)
TimeZoneInfo
类提供了几种共享方法,可用于将时间从一个时区转换为另一个时区。例如,如果您要转换的时间是UTC,则可以使用ConvertTimeFromUtc
方法,如下所示:
Dim convertedTime As Date = TimeZoneInfo.ConvertTimeFromUtc(Date.UtcNow, selectedTimeZone)
或者,如果转换时间在当前的本地时区,那么您可以使用ConvertTime
方法,如下所示:
Dim convertedTime As Date = TimeZoneInfo.ConvertTime(Date.Now, selectedTimeZone)
或者,如果源时间位于另一个时区(非UTC或本地),则可以使用ConvertTime
方法的另一个重载,该方法同时采用源时区和目标时区,像这样:
Dim convertedTime As Date = TimeZoneInfo.ConvertTime(sourceTime, sourceTimeZoneInfo, destinationTimeZoneInfo)