2011-08-21 163 views
0

我正在使用sql服务器数据库,我将时间值存储在日期时间变量中。我正在开发vb.net中的预订系统应用程序。当我想使用datagridview查看已经做好的预订并且通过实现dataadapter和dataset时,它会显示带有系统日期的时间列,该日期在插入记录时随时间保存。现在,我只想在提取数据时仅查看时间字段中的时间......我现在应该做什么?更改数据库中的日期时间类型

回答

0

保持它,因为它是在数据库中。一旦你从数据库中读取数据到VB和分配给一个变量,然后做这样的事情

Dim date1 As Date = #6/1/2008 7:47AM# // this value being read from the db 
Console.WriteLine(date1.ToString()) 

使用以下命令:

' Get date-only portion of date, without its time. 
Dim dateOnly As Date = date1.Date 
' Display date using short date string. 
Console.WriteLine(dateOnly.ToString("d")) 
' Display date using 24-hour clock. 
Console.WriteLine(dateOnly.ToString("g")) 
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm")) 
' The example displays the following output to the console: 
'  6/1/2008 7:47:00 AM 
'  6/1/2008 
'  6/1/2008 12:00 AM 
'  06/01/2008 00:00 

Dim dtmTest As Date 
dtmTest = DateValue(date1) 
0

DateTime值显示与默认备考拟合。您应该在网格视图中为列指定格式。

例子:

bookingDataGridView.Columns["Created"].DefaultCellStyle.Format = "yyyy-MM-dd"; 

这些资源应该可以帮助您指定格式,并选择显示的方式,日期格式字符串你想要的:

How to: Format Data in the Windows Forms DataGridView Control

Standard Date and Time Format Strings

Custom Date and Time Format Strings

+0

三江源解决我的问题 –

2

你也可以做的格式,直接在DataGridView有这样的dataformatstring:

<asp:BoundField DataField="TimeStart" HeaderText="Time In" DataFormatString="{0:t}" /> 
相关问题