如何在vb.net中更改此代码以显示时间

时间:2010-12-10 13:38:39

标签: asp.net vb.net

如何在vb.net中转换此代码,以便在vb.net中仅显示时间而不是日期和时间

            Dim UTCTime As Date = Date.UtcNow
            Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
            TextBox1.Text = IndianTime

5 个答案:

答案 0 :(得分:3)

Dim UTCTime As Date = Date.UtcNow
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
TextBox1.Text = IndianTime.ToString("T")
TextBox2.Text = Now.ToString("T")

答案 1 :(得分:2)

TextBox1.Text = IndianTime.ToString("hh:mm") 'For 12 hr format
TextBox1.Text = IndianTime.ToString("HH:mm") 'For 24 hr format

答案 2 :(得分:1)

Dim formatString As String = String.Format("{0,HH:mm:ss}", IndianTime)

String.Format参考

答案 3 :(得分:1)

试试此代码

    Dim UTCTime As Date = Date.UtcNow
    Dim IndianTime As DateTime = String.Format("{0:hh:mm}", UTCTime.AddHours(5.5))
    TextBox1.Text = IndianTime
    TextBox2.Text = Now

答案 4 :(得分:0)

在ToString()中使用格式说明符,例如:

IndianTime.ToString("hh:mm tt")

应仅输出时间(12小时制,AM或PM)。

自定义格式here的更多示例,并且有一个非常useful list of Date Time format patterns with examples here

整件事:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim UTCTime As DateTime = DateTime.UtcNow
    Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
    TextBox1.Text = IndianTime.ToString("hh:mm tt")
    TextBox2.Text = Now.ToString("hh:mm tt")
End Sub