How to change this code in vb.net to show just the time
how to co开发者_运维问答nvert this code in vb.net to show only time instead of date and time in vb.net
Dim UTCTime As Date = Date.UtcNow
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
TextBox1.Text = IndianTime
Dim UTCTime As Date = Date.UtcNow
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
TextBox1.Text = IndianTime.ToString("T")
TextBox2.Text = Now.ToString("T")
TextBox1.Text = IndianTime.ToString("hh:mm") 'For 12 hr format
TextBox1.Text = IndianTime.ToString("HH:mm") 'For 24 hr format
Dim formatString As String = String.Format("{0,HH:mm:ss}", IndianTime)
String.Format reference
Try this code
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
Use a format specifier in ToString() eg:
IndianTime.ToString("hh:mm tt")
Should output the Time only (in 12 hour clock, with AM or PM).
More examples of custom formatting here, and there's a very useful list of Date Time format patterns with examples here.
Whole thing:
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
精彩评论