用户给两个参数:XXXX年X月,X周。
我需要从这两个参数中获取相应的一个时间段:XXXX年X月X日~XXXX年X月X日。如 例如:
用户输入了:时间:2004年1月,周数:1(第一周)
我要得到:2004-1-1至 2004-1-3.用户输入了:时间:2004年1月,周数:2(第二周)
我要得到:2004-1-4至 2004-1-10.
一经解决,立马给分!真想早点搞定,早点回家过年,否则,呜呜,大家帮忙了。谢了!(注:要用VB语法。我要在水晶报表里用。)
我需要从这两个参数中获取相应的一个时间段:XXXX年X月X日~XXXX年X月X日。如 例如:
用户输入了:时间:2004年1月,周数:1(第一周)
我要得到:2004-1-1至 2004-1-3.用户输入了:时间:2004年1月,周数:2(第二周)
我要得到:2004-1-4至 2004-1-10.
一经解决,立马给分!真想早点搞定,早点回家过年,否则,呜呜,大家帮忙了。谢了!(注:要用VB语法。我要在水晶报表里用。)
Weekday函数可以判断当天是星期几。
Returns a Variant (Date) containing a date to which a specified time interval has been added.SyntaxDateAdd(interval, number, date)The DateAdd function syntax has these named arguments:Part Description
interval Required. String expression that is the interval of time you want to add.
number Required. Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
date Required. Variant (Date) or literal representing date to which the interval is added.
SettingsThe interval argument has these settings:Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second
ResYou can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now.To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").The DateAdd function won't return an invalid date. The following example adds one month to January 31:DateAdd("m", 1, "31-Jan-95")
weekfirst As String
weeklast As String
End TypePrivate Function getweek(ByVal myear As Integer, ByVal mmonth As Integer, ByVal weeknum As Integer) As myweek
Dim mday As Date
mday = DateSerial(myear, mmonth, 1)
Dim i As Integer
i = Weekday(mday, vbSunday)
Dim fday As Date
fday = DateAdd("d", 1 - i, mday)
Dim nday As Date
nday = DateAdd("d", 7 - i, mday)
If weeknum = 1 Then
getweek.weekfirst = CStr(mday)
getweek.weeklast = CStr(nday)
End If
If weeknum > 1 Then
fday = DateAdd("d", (weeknum - 1) * 7, fday)
If month(fday) > mmonth Then
MsgBox "该月没有第" + CStr(weeknum) + "周"
getweek.weekfirst = "输入数据有误"
getweek.weeklast = "输入数据有误"
Exit Function
End If
nday = DateAdd("d", (weeknum - 1) * 7, nday)
If month(nday) > mmonth Then
nday = DateSerial(myear, mmonth + 1, 1)
nday = DateAdd("d", -1, nday)
End If
getweek.weekfirst = CStr(fday)
getweek.weeklast = CStr(nday)
End If
End FunctionPrivate Sub Command1_Click()
'调用
MsgBox getweek(2004, 1, 2).weekfirst '获得该周第一天
MsgBox getweek(2004, 1, 2).weeklast '获得该周最后一天
End Sub
MsgBox getdates("2004-01", 2)
End Sub
Function getdates(ByVal themonth As String, weeks As Integer) As String
On Error Resume Next
Dim days As Integer, allday As New Collection, k As Integer
days = Day(DateSerial(Left(themonth, 4), Right(themonth, 2) + 1, 0))
k = 1
For i = 1 To days
thedate = DateSerial(Left(themonth, 4), Right(themonth, 2), i)
allday.Add thedate, k & Weekday(thedate)
If Weekday(thedate) = 1 Then k = k + 1
Next
For i = 1 To 7
Debug.Print allday(weeks & i)
Next
End Function
Dim EndDate As Date '结束日期
Dim DltWeekDay As Long '一周的第几天
Dim DltDay As Long '两日期的差.
Dim lWeek As Long '周数
'以下取2003年1月第三周的日期间隔.
lWeek = 3
StarDate = "2003/1/1"
DltWeekDay = Weekday(A)
DltDay = lWeek * 7 - (7 - DltWeekDay) - 1
EndDate = DateAdd("d", DltDay, A)
'
MSGBOX STARDATE & ":" & enddate
Option ExplicitPrivate Sub Form_Load()
MsgBox GETWEEK(2004, 1, 3)
End Sub
Function GETWEEK(ByVal YEAR As Integer, ByVal MONTH As Integer, ByVal WEEKS As Integer) As StringIf YEAR < 1 Or YEAR > 9999 Or MONTH < 1 Or MONTH > 12 Then MsgBox "ERR!": Exit Function
On Error Resume Next
Dim DAYS As Integer, ALLDAY As New Collection, I As Integer, K As Integer, TEMPDAY As Date
DAYS = Day(DateSerial(YEAR, MONTH + 1, 0))
K = 1
TEMPDAY = DateSerial(YEAR, MONTH, 1)
ALLDAY.Add TEMPDAY, K & Weekday(TEMPDAY)
For I = 2 To DAYS
TEMPDAY = DateSerial(YEAR, MONTH, I)
If Weekday(TEMPDAY) = 1 Then K = K + 1
ALLDAY.Add TEMPDAY, K & Weekday(TEMPDAY)
Next
For I = 1 To 7
GETWEEK = GETWEEK & vbCrLf & ALLDAY(WEEKS & I)
Next
Set ALLDAY = Nothing
End Function
Option Explicit
Private Sub Command1_Click()
Dim EndDate As Date '结束日期
Dim lDay As Long '用户设定周数的天数
Dim tmpStarDate As Date '每月开始第一天
Dim FirstEndDate As Date '第一周结束日期
Dim StarDate As Date
lDay = (Val(Combo3.Text) - 1) * 7 '第一周以后需加上的天数
tmpStarDate = Combo1.Text & "/" & Combo2.Text & "/" & "1" '设定每月开始第一天
Select Case Weekday(tmpStarDate) '每月开始第一天是星期几
Case 1
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 0) '确定第一周结束日期
Case 2
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 6)
Case 3
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 5)
Case 4
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 4)
Case 5
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 3)
Case 6
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 2)
Case 7
FirstEndDate = Combo1.Text & "/" & Combo2.Text & "/" & Str(1 + 1)
End Select
If lDay = 0 Then '第一周不加
StarDate = tmpStarDate
EndDate = FirstEndDate
Else
lDay = (Val(Combo3.Text) - 1) * 7 '避免下月包括上月日期
StarDate = DateAdd("w", lDay - 6, FirstEndDate)
EndDate = DateAdd("w", lDay, FirstEndDate)
End If
If StarDate = EndDate Then '避免月份第一天为星期天
lDay = Val(Combo3.Text) * 7
StarDate = DateAdd("w", lDay - 6, FirstEndDate)
EndDate = DateAdd("w", lDay, FirstEndDate)
End If
MsgBox StarDate & ":" & EndDate
End SubPrivate Sub Form_Load()
Dim i As Integer
For i = 2003 To 2020
Combo1.AddItem Str(i) '用户选择年
Next i
For i = 1 To 12
Combo2.AddItem Str(i) '用户选择月
Next i
For i = 1 To 5
Combo3.AddItem Str(i) '用户选择周
Next i
End Sub
可以设计时设置:
combo1.text=2003
combo2.text=1
combo3.text=1
--fine--
其实,你可以在数据库中,使用Month,Week,等时间类型函数后,把字段进行处理,如Week(getdate())as DWeek。直接让它作为一个字段,在Crystal Report中就可以直接引用。
SELECT DetePart(Week,GetDate())
直接得到所要月份的星期。GetDate是你需要的时间。
建立Week字段,然后在水晶报表中处理。
在大家的帮助下,我解决了问题,本来rainstormmaster(暴风雨 v2.0)的解决方案
已经使我可以解决问题,由于我是在水晶报表里解决这个问题,用的是SQLSERVER数据
库,后两位的解决方案对我来说,更轻松地实现了这一点,在此特别感谢:
rainstormmaster暴风雨 v2.0,northwolves野性的呼唤,supergirlly别样冷冰寒(名子好酷!),lepeng乐鹏 ,lianyiyong涟漪勇。另:To lepeng乐鹏,兄弟,你太猛了,连为外国人编的都能看出来,在下真真佩服的紧!放分!