【问题标题】:Remove Time from date/time vb.net从日期/时间 vb.net 中删除时间
【发布时间】:2012-11-23 11:50:19
【问题描述】:
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 应该只显示日期,但是当我单击按钮时,文本框会显示日期和时间。例如:2012 年 12 月 1 日凌晨 12:00。

如何删除显示在文本框中的时间?

【问题讨论】:

    标签: vb.net winforms datetime textbox


    【解决方案1】:

    试试……

    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
    

    【讨论】:

    • 显示错误:从字符串“mm/dd/yyyy”到类型“Integer”的转换无效。
    • 好的...那个单元格值的数据类型是什么?
    • 这是一个日期/时间类型,我使用访问作为数据库
    • 当我从 access 数据库中检索数据时,我只希望日期显示在我的文本框中
    • 所以你的意思是它把 null 当作一个日期?为什么要在从数据库中检索一行之前告诉它设置内容?如果这是唯一的问题,请将 Line 包裹在 If dgv1.CurrentRow.Cells(4).Value Isnot Nothing ... End If 中 - 但这不是正确的方法。只有当你有记录要处理时,你才应该调用显示代码
    【解决方案2】:

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

    【讨论】:

    • 我已经尝试了该页面中的示例,但仍然无法正常工作
    【解决方案3】:

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

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

    或者您可以使用DateTime.TryParse,如果转换成功则返回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
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        试试这个

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-26
          • 1970-01-01
          • 1970-01-01
          • 2018-08-20
          • 1970-01-01
          • 2023-01-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多