2016-02-18 152 views
-1

我有一个vb.net数据表 数据是由用户输入,因此没有涉及SQL表。datatable短日期格式

当我存储的“2016年2月1日”到数据表和吐回来了,它显示为“2016年2月1日12:00:00 AM”

什么是最好的方式格式化这回 “2016年2月1日”

Dim table As New DataTable 
table.Columns.Add("Date", GetType(DateTime)) 
table.Rows.Add("2/1/2016") 
debug.print (table.rows(0)("Date").tostring) 

感谢

+0

一个DateTime总是有一个日期和时间。使用显示机制仅显示日期 – Plutonix

+0

我尝试使用getType(日期),但我仍然得到“2/1/2016 12:00:00 AM” – BobNoobGuy

+0

将*总是*是结构的时间元素。 'Debug.Print(Convert.ToDateTime(table.Rows(0)(“Date”))。ToShortDateString)' – Plutonix

回答

1

这里是我找到了一个方式,工作方式:

Dim table As New DataTable 
    table.Columns.Add("Date", GetType(DateTime)) 
    table.Rows.Add("2/1/2016") 
    Debug.Print(CDate(table.Rows(0)("Date").ToString).ToString("d")) 

用户Plutonix也建议。其中也有效!谢谢主席先生

Debug.Print(Convert.ToDateTime(table.Rows(0)("Date")).ToShortDateString) 
+1

这将不会编译根据选项严格 – Plutonix

+0

编辑..使用Cdate使其工作 – BobNoobGuy

0
table.Rows(0)("Date").ToShortDateString() 
+0

这只适用于now.ToShortDateString – BobNoobGuy

+0

呃......不在我的电脑上! (VS2013) – clweeks

+0

我得到这个'ToShortDateString'不是'String'的成员。当我在即时窗口中尝试“测试”.ToShortDateString。本文还建议ToShortDateString仅适用于日期数据类型。 https://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring%28v=vs.110%29.aspx – BobNoobGuy

0

隐蔽为 “MM/DD/YYYY”

CType(table.Rows(0)("Date").ToString, Date).ToShortDateString