以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  天数计算  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=106302)

--  作者:nbsugu_z
--  发布时间:2017/9/6 12:47:00
--  天数计算
Select Case e.DataCol.name
    Case "起日期","止日期"
        If e.DataRow.IsNull("起日期") OrElse e.DataRow.IsNull("止日期") Then
            e.DataRow("天数") = Nothing
        Else
            Dim y,m,d As Integer
            Dim dt1 As Date = e.DataRow("起日期")
            Dim dt2 As Date = e.DataRow("止日期")
            DateYMD(dt1,dt2,y,m,d)
            dt1 = dt1.AddYears(y)
            d= (dt2- dt1).TotalDays()
            e.DataRow("天数") =  d
            
        End If
End Select

以上代码为什么在计算几个月内天数时正确,一年以上数字不动了呢或者是天数不正确了,有什么问题吗


--  作者:有点甜
--  发布时间:2017/9/6 14:34:00
--  

直接计算不就是天数了?


Dim dt1 As Date = "2015-09-05"
Dim dt2 As Date = "2017-09-06"
Dim d= (dt2- dt1).TotalDays()
msgbox(d)

 

--------------------

 

Select Case e.DataCol.name
    Case "起日期","止日期"
        If e.DataRow.IsNull("起日期") OrElse e.DataRow.IsNull("止日期") Then
            e.DataRow("天数") = Nothing
        Else
            Dim d As Integer
            Dim dt1 As Date = e.DataRow("起日期")
            Dim dt2 As Date = e.DataRow("止日期")
            d= (dt2- dt1).TotalDays()
            e.DataRow("天数") =  d           
        End If
End Select
[此贴子已经被作者于2017/9/6 14:34:11编辑过]

--  作者:nbsugu_z
--  发布时间:2017/9/7 10:26:00
--  
不是这样的,起止日期是变动的,因为每一行起止日期不一样,天数列是动态的,所以要写代码
--  作者:有点甜
--  发布时间:2017/9/7 10:29:00
--  
认认真真看2楼。
--  作者:nbsugu_z
--  发布时间:2017/9/7 13:13:00
--  
Select Case e.DataCol.name
    Case "起日期","止日期"
        If e.DataRow.IsNull("起日期") OrElse e.DataRow.IsNull("止日期") Then
            e.DataRow("天数") = Nothing
        Else
       Dim t As TimeSpan
       t = CDate(CurrentTable.Current("止日期"))- CDate(CurrentTable.Current("起日期"))
       e.DataRow("天数") =t.TotalDays
            
        End If
End Select

修改成以上代码成功了,但是我不知道问题出在哪里。

--  作者:有点甜
--  发布时间:2017/9/7 14:42:00
--  
2楼代码同样的意思。你1楼的代码逻辑上就有问题。