如何计算两个日期之间的天数?

时间:2017-05-25 09:07:56

标签: asp.net vb.net

我有一个预订页面和一个包含reservationstart列,reservationend列和numofdays列的预订表。

我已确定客户将选择的两个日期之间的天数,但在更新时没有任何内容存储在表格中。

numofdays的数据类型为datatime,但我已将其更改为int

我首先使用它来声明开始和结束日期:

DayPilotScheduler1.Scale = TimeScale.Manual
Dim start As New Date(Date.Today.Year, 1, 1, 12, 0, 0)
Dim [end] As Date = start.AddYears(1)

这是更新的代码:

Protected Sub DayPilotScheduler1_EventMove(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.EventMoveEventArgs)
    Dim id_Renamed As String = e.Value
    Dim start As Date = e.NewStart
    Dim [end] As Date = e.NewEnd
    Dim resource As String = e.NewResource

    Dim message As String = Nothing

    If Not dbIsFree(id_Renamed, start, [end], resource) Then
        message = "The reservation cannot overlap with an existing reservation."
    ElseIf e.OldEnd <= Date.Today Then
        message = "This reservation cannot be changed anymore."
    ElseIf e.OldStart < Date.Today Then
        If e.OldResource <> e.NewResource Then
            message = "The room cannot be changed anymore."
        Else
            message = "The reservation start cannot be changed anymore."
        End If
    ElseIf e.NewStart < Date.Today Then
        message = "The reservation cannot be moved to the past."
    Else
        dbUpdateEvent(id_Renamed, start, [end], resource)
        'message = "Reservation moved.";
    End If

    LoadResourcesAndEvents()
    DayPilotScheduler1.UpdateWithMessage(message)
End Sub

Private Sub dbUpdateEvent(ByVal id As String, ByVal start As Date, ByVal [end] As Date, ByVal resource As String)
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionStringLocal").ConnectionString)
        con.Open()
        Dim numOfDay As Integer = CInt(([end] - start).TotalDays())


        Dim cmd As New SqlCommand("UPDATE [Reservation] SET ReservationStart = @start, ReservationEnd = @end, RoomId = @resource,numofday=@numofday WHERE ReservationId = @id", con)
        cmd.Parameters.AddWithValue("id", id)
        cmd.Parameters.AddWithValue("start", start)
        cmd.Parameters.AddWithValue("end", [end])
        cmd.Parameters.AddWithValue("resource", resource)
        cmd.Parameters.Add("numofday", SqlDbType.Int).Value = numOfDay
        cmd.ExecuteNonQuery()
    End Using
End Sub

数据库表结构的屏幕截图:

enter image description here

3 个答案:

答案 0 :(得分:0)

Math.floor(Math.abs(new Date(timestringone) - new Date(timestringtwo))/(1000*60*60*24))

简单地减去日期会返回它们之间的毫秒时间。如果第一次是在第二次之前值为负,那么我使用Math.abs使其成为绝对值。然后我们分为1000毫秒= 1秒,60秒= 1分钟,60分钟= 1小时,24小时= 1天,并将其降至整天。需要两个有效的时间串(timestringone和timestringtwo)。

这是一个javascript解决方案,因为你已经包含了js标签......

答案 1 :(得分:0)

我不确定VB.Net,但您可以使用Type&#34; TimeSpan&#34;的对象在C#中轻松完成它。例如:让我们假设我们想知道开始和结束之间的天数。日期时间类型的值并在控制台窗口中显示,然后我可能会写:

DateTime start=DateTime.MinValue;
DateTime end=DateTime.MaxValue;
TimeSpan span=end-start;
Console.WriteLine( "There're {0} days between {1} and {2}" , span.TotalDays, start.ToString(), end.ToString() );

答案 2 :(得分:0)

OP在使用.Days结构上的TimeSpan属性时遇到问题。我认为这可能有所帮助:

Dim numOfDay As Integer = CInt(([end] - start).TotalDays())

输出结果为:

365

继续使用您的参数,我认为您将从使用.Add并指定数据类型中受益:

cmd.Parameters.Add("@id", SqlDbType.Int).Value = id
cmd.Parameters.Add("@start", SqlDbType.Date).Value = start
cmd.Parameters.Add("@end", SqlDbType.Date).Value = [end]
cmd.Parameters.Add("@resource", SqlDbType.Int).Value = CInt(resource)
cmd.Parameters.Add("@numofday", SqlDbType.Int).Value = numOfDay
  

请注意,您可能需要更改SqlDbType。我已经做了一个假设。

我还会为UsingSqlConnection实施SqlCommand。对我来说这只是一个很好的做法,代码确实更好。我还会对所有参数使用.Add重载。

Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionStringLocal").ConnectionString),
      cmd As New SqlCommand("UPDATE [Reservation] SET ReservationStart = @start, ReservationEnd = @end, RoomId = @resource, numofday = @numofday WHERE ReservationId = @id", con)

    con.Open()

    cmd.Parameters.Add("@id", SqlDbType.Int).Value = id
    cmd.Parameters.Add("@start", SqlDbType.Date).Value = start
    cmd.Parameters.Add("@end", SqlDbType.Date).Value = [end]
    cmd.Parameters.Add("@resource", SqlDbType.Int).Value = CInt(resource)
    cmd.Parameters.Add("@numofday", SqlDbType.Int).Value = CInt(([end] - start).TotalDays())

    Dim rowsAffected As Integer = cmd.ExecuteNonQuery()
    If rowsAffected = 0 Then
        'nothing updated
    Else
        'something updated
    End If

End Using