以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  两个日期间天数的计算  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=72502)

--  作者:xiaohuli
--  发布时间:2015/7/31 0:17:00
--  两个日期间天数的计算

在绘制甘特图的时候,我写了如下代码,但代码总是有错误。周期列是表中没有的,我想通过查询表中的开始日期和结束日期来计算出周期,但是计算周期的时候不对,请老师们帮忙看看。

If DrawGannt Then
    Dim r As Row  = Tables("任务_Table1").Rows(e.Row.Index)
    Dim dt1 As Date = r("开始日期")
    Dim dt2 As Date = r("结束日期")
    Dim dt As Date = e.Col.Name.Replace("年","-").Replace("月_","-")
    If dt>=dt1 AndAlso dt<=dt2 Then
        e.StartDraw()
        If dt < Date.Today Then
            e.Graphics.FillRectangle(Brushes.Green,e.x ,e.y + 5, e.Width, e.Height - 10)
        Else
            e.Graphics.FillRectangle(Brushes.Red,e.x ,e.y + 3, e.Width, e.Height - 7)
        End If
        If r.IsNull("计划任务") = False Then
            Dim tp As TimeSpan
            Dim dr As Date
            tp = (dr("开始日期") - dr("结束日期")).TotalDays()
         
            Dim day As Double = tp / r("计划任务") * r("累计完成")
            If dt <= dt1.AddHours(24 * day) Then
                Dim d As Integer = math.Floor(day)
                If (dt-dt1).Days < d Then \'满格
                    e.Graphics.FillRectangle(Brushes.Yellow,e.x ,e.y + 8, e.Width, e.Height - 16)
                Else
                    Dim width As Integer = (day - d) * e.width
                    e.Graphics.FillRectangle(Brushes.Yellow,e.x ,e.y + 8, width, e.Height - 16)
                End If
            End If
        End If
        e.EndDraw()
    End If
End If
\'
\'
\'
\'


--  作者:xiaohuli
--  发布时间:2015/7/31 9:35:00
--  

老师们,帮我看看我的问题。


--  作者:大红袍
--  发布时间:2015/7/31 9:53:00
--  
上传具体例子,代码看不出问题。
--  作者:xiaohuli
--  发布时间:2015/7/31 10:00:00
--  

老师,代码是有问题的,保存的时候出现错误。


图片点击可在新窗口打开查看此主题相关图片如下:1.png
图片点击可在新窗口打开查看

--  作者:大红袍
--  发布时间:2015/7/31 10:26:00
--  

 从另一个表查找?

 

 参考 http://www.foxtable.com/help/topics/0396.htm

 


--  作者:xiaohuli
--  发布时间:2015/7/31 10:36:00
--  

老师,我的查询表中是没有周期的,我想通过结束日期-开始日期来计算出周期,然后按照周期去计算。


--  作者:有点蓝
--  发布时间:2015/7/31 10:40:00
--  
有时候日期计算时会出错
http://www.foxtable.com/help/index.html?n=1517.htm
--  作者:大红袍
--  发布时间:2015/7/31 10:58:00
--  

那直接写

 

tp = dt2 - dt1

Dim day As Double = tp.TotalDays / r("计划任务") * r("累计完成")


--  作者:狐表开发
--  发布时间:2015/7/31 11:30:00
--  

If DrawGannt Then
    Dim r As Row  = Tables("任务_Table1").Rows(e.Row.Index)
    Dim dt1 As Date = r("开始日期")
    Dim dt2 As Date = r("结束日期")
    Dim dt As Date = e.Col.Name.Replace("年","-").Replace("月_","-")
    If dt>=dt1 AndAlso dt<=dt2 Then
        e.StartDraw()
        If dt < Date.Today Then
            e.Graphics.FillRectangle(Brushes.Green,e.x ,e.y + 5, e.Width, e.Height - 10)
        Else
            e.Graphics.FillRectangle(Brushes.Red,e.x ,e.y + 3, e.Width, e.Height - 7)
        End If
        If r.IsNull("计划任务") = False Then
            Dim tp As TimeSpan
            Dim dr As Date
            tp = dr("开始日期") - dr("结束日期")        
            Dim day As Double = tp.TotalDays() / r("计划任务") * r("累计完成")
            If dt <= dt1.AddHours(24 * day) Then
                Dim d As Integer = math.Floor(day)
                If (dt-dt1).Days < d Then \'满格
                    e.Graphics.FillRectangle(Brushes.Yellow,e.x ,e.y + 8, e.Width, e.Height - 16)
                Else
                    Dim width As Integer = (day - d) * e.width
                    e.Graphics.FillRectangle(Brushes.Yellow,e.x ,e.y + 8, width, e.Height - 16)
                End If
            End If
        End If
        e.EndDraw()
    End If
End If