Remove time from vb.net date / time

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form2.Show() Form2.TextBox1.Text = dgv1.CurrentRow.Cells(0).Value.ToString Form2.TextBox2.Text = dgv1.CurrentRow.Cells(1).Value.ToString Form2.TextBox3.Text = dgv1.CurrentRow.Cells(2).Value.ToString Form2.TextBox4.Text = dgv1.CurrentRow.Cells(3).Value.ToString Form2.TextBox5.Text = dgv1.CurrentRow.Cells(4).Value.ToString Form2.TextBox6.Text = dgv1.CurrentRow.Cells(5).Value.ToString Form2.TextBox7.Text = dgv1.CurrentRow.Cells(6).Value.ToString 

textbox5 is one that should only show the date, but when I click the button, the text box shows me the date and time. ex: 12/01/12 12:00.

how can i remove time from showing in textbox?

+7
source share
5 answers

Since CurrentCells (4) .Value is an object, try translating it to DateTime and then converting using the ToShortDateString Method

 Form2.TextBox5.Text = CType(dgv1.CurrentRow.Cells(4).Value, DateTime).ToShortDateString 

or you can use DateTime.TryParse , which will return true if the conversion is successful

 Dim tempDate As Date If DateTime.TryParse(CStr(dgv.CurrentRow.Cells(4).Value), tempDate) Then Form2.TextBox5.Text = tempDate.ToShortDateString Else Form2.TextBox5.Text = "Invalid Date" End If 
+8
source

Try ...

 If dgv1.CurrentRow.Cells(4).Value IsNot Nothing Form2.TextBox5.Text = String.Format("{0:dd/mm/YYYY}", dgv1.CurrentRow.Cells(4).Value) Else Form2.TextBox5.Text = "" End If 
+2
source

There is a function of the date and time format. check out http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.71).aspx

+1
source

I just deleted the .ToString in this line:

 Form2.TextBox5.Text = dgv1.CurrentRow.Cells(4).Value.ToString 

When I removed .ToString, it just shows a short date in the date text box

+1
source

try it

 declare MyTime as datetime Mytime = dateadd(day, -datediff(day, 0, Date.now), Date.now) 
0
source

All Articles