我想比较一个时间值和日期时间值

问题描述:

我想过滤我的结果相对于日期和时间....我有日期存储在一个单独的字段是datetime数据类型...另一个字段存储时间值,但因为它是一个日期时间数据类型,所以它存储当前系统日期以及输入的时间。现在,当我使用日期筛选结果时,它运行良好,并显示特定时期之间的结果,例如2011年10月12日至2011年12月15日期间的日期...但是,当我想要显示关于时间的结果时, t显示任何结果...我想比较一个时间值和日期时间值

我正在使用dataadapter和数据集从sql server数据库中检索记录...请告诉我比较从文本框中获取的时间值与存储在数据库中的日期时间值的解决方案我得到这样的下午12:00和下午6:00

更加清晰之间的具体时间之间的结果:

其实我有存储在数据库中的日期时间值。我从文本框中得到的是一个时间值。我想从数据库中提取给定时间值之间的结果....为了更加简洁,我的应用程序是一个体育俱乐部的预订系统,并提供一个选项来查看alreaady预订。在这里,我提供了两个选项可以查看特定游戏的所有预订或筛选预订。在过滤预订中,有一个选项是过滤日期和时间...日期选项运行正常,但问题在于时间部分...我提供两次但无法查看它们之间的预订...

我的代码是:

 Dim prmtimefrom As New SqlParameter("@booking_time", SqlDbType.DateTime) 
     prmtimefrom.Value = TextBox3.Text 

     Dim prmtimeto As New SqlParameter("@booking_tim", SqlDbType.DateTime) 
     prmtimeto.Value = TextBox4.Text 




     Dim da As New SqlDataAdapter("select * from Bookings where booking_time Between @booking_time AND @booking_tim AND game = " & x, con) ' x is the name of a specific game 

     da.SelectCommand.Parameters.Add(prmtimefrom) 
     da.SelectCommand.Parameters.Add(prmtimeto) 
     da.Fill(ds, "Bookings") 

我不确定你是问如何做到这一点在vb.net,或如何从SQL数据库中做到这一点。为了在.NET中进行筛选,您需要使用DateTime.TimeOfDay属性。一个简单的例子是:

'Set the from time to start Jan 1, 2011 at 8:00am 
Dim startTime as DateTime = New DateTime(2011, 1, 1, 8, 0, 0) 

'Set the to time to end Feb 3, 2006 at 1:30pm 
Dim endTime as DateTime = New DateTime(2006, 2, 3, 13, 30, 0) 

'Compare the current time to the start/end times 
'Notice that the dates are ignored, we are only comparing times 
If Now.TimeOfDay >= startTime.TimeOfDay AndAlso Now.TimeOfDay <= endTime.TimeOfDay Then 
    MsgBox("Within time span") 
Else 
    MsgBox("Outside of time span") 
End If 

事情是这样的:

Dim starting = TimeSpan.Parse("12:00") 
Dim ending = TimeSpan.Parse("18:00") 

Dim query = _ 
    From dr in dt.Rows.Cast(Of DataRow)() _ 
    Let tod = dr.Field(Of DateTime)(0).TimeOfDay _ 
    Where tod >= starting AndAlso tod < ending _ 
    Select dr