有关星期的问题,急用,一解决就给分,在线等!!!

chater 2004-01-16 04:43:20
用户给两个参数: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语法。我要在水晶报表里用。)
...全文
101 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
chater 2004-01-17
  • 打赏
  • 举报
回复
谢谢大家的回答,大家都这么热心地帮我,真的很感激。
在大家的帮助下,我解决了问题,本来rainstormmaster(暴风雨 v2.0)的解决方案
已经使我可以解决问题,由于我是在水晶报表里解决这个问题,用的是SQLSERVER数据
库,后两位的解决方案对我来说,更轻松地实现了这一点,在此特别感谢:
rainstormmaster暴风雨 v2.0,northwolves野性的呼唤,supergirlly别样冷冰寒(名子好酷!),lepeng乐鹏 ,lianyiyong涟漪勇。

另:To lepeng乐鹏,兄弟,你太猛了,连为外国人编的都能看出来,在下真真佩服的紧!

放分!
lianyiyong 2004-01-17
  • 打赏
  • 举报
回复
直接在SQL Server 使用
SELECT DetePart(Week,GetDate())
直接得到所要月份的星期。GetDate是你需要的时间。
建立Week字段,然后在水晶报表中处理。
supergirlly 2004-01-17
  • 打赏
  • 举报
回复
HI:
其实,你可以在数据库中,使用Month,Week,等时间类型函数后,把字段进行处理,如Week(getdate())as DWeek。直接让它作为一个字段,在Crystal Report中就可以直接引用。
kimurakenshin 2004-01-17
  • 打赏
  • 举报
回复
up up
mmcgzs 2004-01-17
  • 打赏
  • 举报
回复
northwolves(野性的呼唤) 修改后的程序可以用
lepeng 2004-01-17
  • 打赏
  • 举报
回复
请教:这是为外国人编的吗?

还是大佬厉害。
lepeng 2004-01-17
  • 打赏
  • 举报
回复

可以设计时设置:
combo1.text=2003
combo2.text=1
combo3.text=1


--fine--
lepeng 2004-01-17
  • 打赏
  • 举报
回复
简陋,向各位学习


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 Sub

Private 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
88391788 2004-01-16
  • 打赏
  • 举报
回复
mark
northwolves 2004-01-16
  • 打赏
  • 举报
回复
修改了一下:
Option Explicit

Private 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 String

If 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
华芸智森 2004-01-16
  • 打赏
  • 举报
回复
以下程序已验证通过.

Dim StarDate As Date'开始日期
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
northwolves 2004-01-16
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
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
rainstormmaster 2004-01-16
  • 打赏
  • 举报
回复
Private Type myweek
weekfirst As String
weeklast As String
End Type

Private 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 Function

Private Sub Command1_Click()
'调用
MsgBox getweek(2004, 1, 2).weekfirst '获得该周第一天
MsgBox getweek(2004, 1, 2).weeklast '获得该周最后一天
End Sub

liyd1978 2004-01-16
  • 打赏
  • 举报
回复
DateAdd Function


Returns a Variant (Date) containing a date to which a specified time interval has been added.

Syntax

DateAdd(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.


Settings

The 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


Remarks

You 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")

sqfeiyu 2004-01-16
  • 打赏
  • 举报
回复
自己编个函数,从该月第一天算起,循环几个星期天
Weekday函数可以判断当天是星期几。
zfl2k 2004-01-16
  • 打赏
  • 举报
回复
自己编个函数,从该月第一天算起,循环几个星期天不就行了。
代码我懒得写。
chater 2004-01-16
  • 打赏
  • 举报
回复
我用的是SQLServer数据库。
macroyan 2004-01-16
  • 打赏
  • 举报
回复
ORACLE数据库中应该有相应的函数!
j4sxw 2004-01-16
  • 打赏
  • 举报
回复
没搞过
cqq_chen 2004-01-16
  • 打赏
  • 举报
回复
查CSDN中的FORMAT

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧