Reputation: 524
how to convert 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
Upvotes: 0
Views: 3715
Reputation: 9459
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
Upvotes: 0
Reputation: 3181
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
Upvotes: 1
Reputation: 17973
Dim formatString As String = String.Format("{0,HH:mm:ss}", IndianTime)
String.Format reference
Upvotes: 1
Reputation: 1177
TextBox1.Text = IndianTime.ToString("hh:mm") 'For 12 hr format
TextBox1.Text = IndianTime.ToString("HH:mm") 'For 24 hr format
Upvotes: 2
Reputation: 63562
Dim UTCTime As Date = Date.UtcNow
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
TextBox1.Text = IndianTime.ToString("T")
TextBox2.Text = Now.ToString("T")
Upvotes: 3