从日期/时间 vb.net 中删除时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13726865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:27:27  来源:igfitidea点击:

Remove Time from date/time vb.net

vb.netwinformsdatetimetextbox

提问by Rara Arar

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 the one that should only show the date but when i click the button, the textbox shows me the date and time. ex: 12/01/12 12:00 AM.

textbox5 应该只显示日期,但是当我单击按钮时,文本框会显示日期和时间。例如:12/01/12 12:00 AM。

how can i remove the time from showing up into the textbox?

我怎样才能删除出现在文本框中的时间?

回答by Mark Hall

Since the CurrentCells(4).Value is an object try casting it to a DateTimethen convert using the ToShortDateString Method

由于 CurrentCells(4).Value 是一个对象,请尝试将其DateTime转换为一个对象,然后使用ToShortDateString Method

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

or you can use DateTime.TryParsewhich will return true if the conversion is succesfull

或者您可以使用DateTime.TryParsewhich 将在转换成功时返回 true

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

回答by Basic

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

回答by Less

there is a datetime format function. check this http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.71).aspx

有一个日期时间格式功能。检查这个http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.71).aspx

回答by Rara Arar

I just removed the .ToString in this line:

我刚刚删除了这一行中的 .ToString :

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

When i removed the .ToString it just shows the short date in the date textbox

当我删除 .ToString 时,它只在日期文本框中显示短日期

回答by AlMounkez

try this

尝试这个

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