以文本方式查看主题

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

--  作者:老有所乐
--  发布时间:2010/11/30 7:20:00
--  日期计算

求表A“日期AB列相隔时间”列代码,即日期A与日期B两列相隔时间的代码。

谢谢!

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:管理项目1.table


--  作者:狐狸爸爸
--  发布时间:2010/11/30 8:07:00
--  

DataColChanged事件:

 

Select Case e.DataCol.Name
    Case "日期A","日期B"
        If e.DataRow.IsNull("日期A") OrElse e.DataRow.IsNull("日期B") Then
            e.DataRow("日期AB列相隔时间") = Nothing
        Else
            Dim y,m,d As Integer
            Dim d1 As Date = e.DataRow("日期A")
            Dim d2 As Date = e.DataRow("日期B")
            y = d2.Year - d1.Year
            m = d2.Month - d1.Month
            d = d2.day - d1.day
            If m = 0 OrElse (m = 0 AndAlso d < 0) Then
                y = y - 1
                m = m + 12
            End If
            If d < 0 Then
                m = m - 1
                d = Date.DaysInMonth(d1.Year,d1.Month) + d
            End If
            Dim s As String
            If y > 0 Then
                s = y & "年"
            End If
            If m > 0 Then
                s = s & m & "个月"
            End If
            If d > 0 Then
                s = s & d & "日"
            End If
            e.DataRow("日期AB列相隔时间") = s      
        End If
End Select

[此贴子已经被作者于2010-11-30 9:10:10编辑过]

--  作者:老有所乐
--  发布时间:2010/11/30 11:17:00
--  

谢谢贺老师!

还有一个问题,如果两日期列为同年同月 显示的就为12个月  如    日期A 为2010-11-28  日期B  为2010-11-30  结果成了12个月2日.正确结果应该是 2日,这不知道是哪里的问题。

[此贴子已经被作者于2010-11-30 11:19:20编辑过]

--  作者:狐狸爸爸
--  发布时间:2010/11/30 11:28:00
--  

是我手误,将<打成=了,正确的:

 

Select Case e.DataCol.Name
    Case "日期A","日期B"
        If e.DataRow.IsNull("日期A") OrElse e.DataRow.IsNull("日期B") Then
            e.DataRow("日期AB列相隔时间") = Nothing
        Else
            Dim y,m,d As Integer
            Dim d1 As Date = e.DataRow("日期A")
            Dim d2 As Date = e.DataRow("日期B")
            y = d2.Year - d1.Year
            m = d2.Month - d1.Month
            d = d2.day - d1.day
            If m < 0 OrElse (m = 0 AndAlso d < 0) Then
                y = y - 1
                m = m + 12
            End If
            If d < 0 Then
                m = m - 1
                d = Date.DaysInMonth(d1.Year,d1.Month) + d
            End If
            Dim s As String
            If y > 0 Then
                s = y & "年"
            End If
            If m > 0 Then
                s = s & m & "个月"
            End If
            If d > 0 Then
                s = s & d & "日"
            End If
            e.DataRow("日期AB列相隔时间") = s      
        End If
End Select


--  作者:老有所乐
--  发布时间:2010/11/30 11:54:00
--  

谢谢!这个就完全正确。

 


--  作者:lihe60
--  发布时间:2010/11/30 12:30:00
--  

能不能增加本年第一天、上月最后一天、本月第一天、本月最后一天、本年最后一天的函数。


--  作者:mr725
--  发布时间:2010/11/30 13:03:00
--  
以下是引用lihe60在2010-11-30 12:30:00的发言:

能不能增加本年第一天、上月最后一天、本月第一天、本月最后一天、本年最后一天的函数。

近似的帮助中可以找到啊~  DaysInMonth

[此贴子已经被作者于2010-11-30 13:03:42编辑过]

--  作者:czy
--  发布时间:2010/11/30 13:09:00
--  

好像没什么必要,太简单了,自己写一下也无妨。

 

Dim d As Date = Date.Today
Output.Show("本年第一天:" & New Date(d.Year,1,1))
Output.Show("本年最后一天:" & New Date(d.Year,12,Date.DaysInMonth(d.Year,1)))
Output.Show("本月第一天:" & New Date(d.Year,d.Month,1))
Output.Show("本月最后一天:" & New Date(d.Year,12,Date.DaysInMonth(d.Year,d.Month)))


--  作者:czy
--  发布时间:2010/11/30 17:29:00
--  
老六,要能给时段型数据加上Years和Months属性就好了,这样我们就无需这么去折腾了。
--  作者:狐狸爸爸
--  发布时间:2010/11/30 17:46:00
--  
呵呵,这怎么加啊,一个月按多少天算呢?