以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  怎么判定上周有购买过电脑,代码要怎么写  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=188157)

--  作者:F1216310
--  发布时间:2023/9/5 13:42:00
--  怎么判定上周有购买过电脑,代码要怎么写
产品 购买日期
电脑 8.29
电脑 8.30
电脑 9.5

--  作者:有点蓝
--  发布时间:2023/9/5 13:52:00
--  
参考:http://www.foxtable.com/webhelp/topics/2720.htm


Dim y As Integer = Date.Today.Year
Dim w As Integer = Date.Today.DayOfWeek \'算出今天是星期几
Dim dt1 As Date = Date.Today.AddDays( - w) \'获取本周的第一天
Dim dt2 As Date = Date.Today.AddDays(6 - w) \'获取本周的最后一天
Dim Filter As String
Filter = "购买日期 >= #" & dt1 & "# And 购买日期 <= #" & dt2 & "#"
msgbox("本周条件:" & Filter)

Dim Filter1 As String = "购买日期 >= #" & dt1.AddDays( - 7) & "# And 购买日期 <= #" & dt2.AddDays( - 7) & "#"
msgbox("上周条件:" & Filter1)

--  作者:listfor
--  发布时间:2023/9/5 14:53:00
--  
Imports System.Globalization

Public Sub CheckComputerPurchaseLastWeek()
Dim purchaseRecords As List(Of Dictionary(Of String, Object)) = New List(Of Dictionary(Of String, Object))() From {
New Dictionary(Of String, Object) From {{"product", "电脑"}, {"purchaseDate", New DateTime(2023, 8, 29)}, },
New Dictionary(Of String, Object) From {{"product", "电脑"}, {"purchaseDate", New DateTime(2023, 8, 30)}, },
New Dictionary(Of String, Object) From {{"product", "电脑"}, {"purchaseDate", New DateTime(2023, 9, 5)}, }
}

Dim currentDate As DateTime = DateTime.Now
Dim lastWeekStart As DateTime = currentDate.AddDays(-7).Date.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)

Dim hasPurchase As Boolean = False

For Each record In purchaseRecords
If record("purchaseDate").ToString("MM/dd/yyyy", CultureInfo.InvariantCulture) >= lastWeekStart Then
hasPurchase = True
Exit For
End If
Next

If hasPurchase Then
Console.WriteLine("上周有购买电脑")
Else
Console.WriteLine("上周没有购买电脑")
End If
End Sub
 
--  作者:有点蓝
--  发布时间:2023/9/5 15:04:00
--  
如果是ChatGPT提问的代码,以后建议还是不要发上来了。