以文本方式查看主题

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

--  作者:实话实说
--  发布时间:2018/7/27 13:42:00
--  根据日期计算第几周
如何用代码根据第一行的日期开始计算第几周(如图效果)

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

--  作者:新福星
--  发布时间:2018/7/27 13:51:00
--  
 

 

 

Dim rq As Date = Args(0)   \'类别

 

 

Dim ia As  Integer

Dim ib As Integer

Dim ic As Integer

Dim iy As Integer

Dim ix As Integer

 

 

 

Dim rq1 As Date

 

 

rq1= "#1/1/" & rq.Year & "#"

 

ia = rq.DayOfYear

ib= rq1.DayOfWeek

 

ic=ia+ib \'-2

iy = ic Mod 7

ix= ic \\7

If iy<>0 Then ix=ix+1

Return ix


--  作者:实话实说
--  发布时间:2018/7/27 14:22:00
--  
不是从1月1日开始,是从第一行开始,输入日期后自动显示第几周.
--  作者:有点甜
--  发布时间:2018/7/27 14:35:00
--  

参考代码

 

Dim d As Date = "2018-07-28"
Dim start As Date = "2018-07-20" \'起始日期
Dim w As Integer = Math.Ceiling(((d-start).TotalDays + 1) / 7)
msgbox(w)


--  作者:实话实说
--  发布时间:2018/7/27 15:05:00
--  
以下代码w都等于2。另外,起始日期是不确定的。

Dim d As Date = "2018-07-28" 
Dim start As Date = "2018-07-20" \'起始日期
Dim w As Integer = Math.Ceiling(((d-start).TotalDays + 1) / 7)
\'msgbox(w)
If e.DataCol.Name = "日期" Then 
e.DataRow("第几周") = w
End If

--  作者:有点甜
--  发布时间:2018/7/27 15:12:00
--  

动态赋值

 

If e.DataCol.Name = "日期" Then
   
    Dim fdr As DataRow = e.DataTable.Find("", "日期")
   
    Dim d As Date = e.DataRow("日期")
    Dim start As Date = fdr("日期")
    Dim w As Integer = Math.Ceiling(((d-start).TotalDays + 1) / 7)
    e.DataRow("第几周") = w
End If

 

[此贴子已经被作者于2018/7/27 15:57:12编辑过]

--  作者:实话实说
--  发布时间:2018/7/27 15:46:00
--  
6楼代码得不到想要的结果
--  作者:有点甜
--  发布时间:2018/7/27 15:57:00
--  
以下是引用实话实说在2018/7/27 15:46:00的发言:
6楼代码得不到想要的结果

 

If e.DataCol.Name = "日期" Then
   
    Dim fdr As DataRow = e.DataTable.Find("", "日期")
   
    Dim d As Date = e.DataRow("日期")
    Dim start As Date = fdr("日期")
    Dim w As Integer = Math.Ceiling(((d-start).TotalDays + 1) / 7)
    e.DataRow("第几周") = w
End If

 

[此贴子已经被作者于2018/7/27 15:57:22编辑过]

--  作者:实话实说
--  发布时间:2018/7/27 16:13:00
--  
表A只能一行一行的增加,行尾不能有空行,否则计算错误
--  作者:有点甜
--  发布时间:2018/7/27 17:15:00
--  

If e.DataCol.Name = "日期" Then
    Dim d As Date = e.DataRow("日期")
    If d = Nothing Then
        e.DataRow("第几周") = Nothing
    Else
        Dim fdr As DataRow = e.DataTable.Find("", "日期")
        Dim start As Date = fdr("日期")
        Dim w As Integer = Math.Ceiling(((d-start).TotalDays + 1) / 7)
        e.DataRow("第几周") = w
    End If
End If