最近输入大量数据时用到的随机产生日期的函数,拿来分享下
本帖最后由 wz403692767 于 2011-8-17 15:41 编辑之前网上搜罗半天没找到相关的,自己动手写了,可能有BUG,自己用没啥问题,现在拿出来分享下,需要的拿去吧
功能就是产生大量随机数据时需要产生的随机日期,在脚本中调用 RandomDate("2010-1-1","2011-8-16") 就可以了
'========================================
' 函数名:RandomDate
' 参数:DateMin, DateMax
' 返回范围内的随机日期,输入输出的格式有"2000/1/1","2000-1-1"
'=========================================
Function RandomDate(DateMin, DateMax)
Dim nYearMin,nMonthMin,nDayMin,nYearMax,nMonthMax,nDayMax
Dim nYear,nMonth,nDay
Dim DayNum
'起始日期
nYearMin = Year(DateMin)
nMonthMin = Month(DateMin)
nDayMin = Day(DateMin)
'终止日期
nYearMax = Year(DateMax)
nMonthMax = Month(DateMax)
nDayMax = Day(DateMax)
'随机生成一个年份
nYear = RandomNumber(nYearMin,nYearMax)
'年份在上下限之内
If nYear > nYearMin And nYear < nYearMax Then
'随机生成一个月份
nMonth = RandomNumber(1,12)
DayNum = GetDayOfCurrentMonth(nYear,nMonth)
'根据月份随机产生日子
nDay = RandomNumber(1,DayNum)
'年份等于下限
ElseIf nYear = nYearMin And nYear <> nYearMax Then
'随机生成的月份大于等于下限的月份
nMonth = RandomNumber(nMonthMin,12)
DayNum = GetDayOfCurrentMonth(nYear,nMonth)
'相等
If nMonth = nMonthMin Then
nDay = RandomNumber(nDayMin+1,DayNum)
'大于
ElseIf nMonth > nMonthMin Then
nDay = RandomNumber(1,DayNum)
End If
'年份等于上限
ElseIf nYear = nYearMax And nYear <> nYearMin Then
'随机生成的月份小于等于上限的月份
nMonth = RandomNumber(1,nMonthMax)
DayNum = GetDayOfCurrentMonth(nYear,nMonth)
'相等
If nMonth = nMonthMax Then
nDay = RandomNumber(1,nDayMax-1)
'大于
ElseIf nMonth < nMonthMax Then
nDay = RandomNumber(1,DayNum)
End If
'同年
ElseIf nYear = nYearMin And nYear = nYearMax Then
nMonth = RandomNumber(nMonthMin,nMonthMax)
DayNum = GetDayOfCurrentMonth(nYear,nMonth)
'月份上下限之内
If nMonth > nMonthMin And nMonth < nMonthMax Then
nDay = RandomNumber(1,DayNum)
'等于月下限
ElseIf nMonth = nMonthMin And nMonth <> nMonthMax Then
nDay = RandomNumber(nDayMin+1,DayNum)
'等于月上限
ElseIf nMonth = nMonthMax And nMonth <> nMonthMin Then
nDay = RandomNumber(1,nDayMax-1)
'同月
ElseIf nMonth = nMonthMin And nMonth = nMonthMax Then
nDay = RandomNumber(nDayMin,nDayMax)
End If
End If
'生成结果并处理字符串
RandomDate = Cstr(nYear) & "-" & Cstr(nMonth) & "-" & Cstr(nDay)
End Function
'============================================
' 函数名:IsLeapYear
' 参数:nYear
' 返回是否是闰年,是为True,否为False
'============================================
Function IsLeapYear(nYear)
Dim Flag
If nYear <> "" Then
If (nYear Mod 4 = 0) And (nYear Mod 100 = 0) Or (nYear) Mod 400 = 0 Then
Flag = True
Else
Flag = False
End If
End If
IsLeapYear = Flag
End Function
'===========================================
' 函数名:GetDayOfCurrentMonth
' 参数:nYear,nMonth
' 返回当年当月的天数
'===========================================
Function GetDayOfCurrentMonth(nYear,nMonth)
Select Case nMonth
Case 1
GetDayOfCurrentMonth = 31
Case 2
If IsLeapYear(nYear) Then
GetDayOfCurrentMonth = 29
Else
GetDayOfCurrentMonth = 28
End If
Case 3
GetDayOfCurrentMonth = 31
Case 4
GetDayOfCurrentMonth = 30
Case 5
GetDayOfCurrentMonth = 31
Case 6
GetDayOfCurrentMonth = 30
Case 7
GetDayOfCurrentMonth = 31
Case 8
GetDayOfCurrentMonth = 31
Case 9
GetDayOfCurrentMonth = 30
Case 10
GetDayOfCurrentMonth = 31
Case 11
GetDayOfCurrentMonth = 30
Case 12
GetDayOfCurrentMonth = 31
End Select
End Function Function RandomDate(minDate, maxDate)
lenDay = DateDiff("d", minDate, maxDate)
rndDay = RandomNumber(0, lenDay)
RandomDate = DateAdd( "d", rndDay, minDate)
End Function
For i = 1 to 10
msgbox RandomDate("2010-1-1", "2011-8-13")
Next 楼主,没必要像你写的这么复杂
而且最好加上Randomize来初始化你的随机函数
页:
[1]