使用VB脚本生成随机日期时间

时间:2013-09-24 16:31:40

标签: vbscript

如何使用VB脚本在开始日期和结束日期之间随机生成日期和时间。我们必须从用户那里获取信息 示例:startDate:15/09/2013 9:00:00 Enddate 21/09/2013 15:00:00

输出:随机日期:17/09/2013 12:00:00

1 个答案:

答案 0 :(得分:4)

为startDate添加一个小于秒的日期之差的随机秒数:

  Dim StartDate : StartDate = #15/09/2013 9:00:00#
  Dim Enddate   : Enddate   = #21/09/2013 15:00:01#
  Dim SecDiff   : SecDiff   = DateDiff("s", StartDate, Enddate)
  WScript.Echo StartDate, Enddate, SecDiff
  Dim n
  For n = 1 To 20
      WScript.Echo DateAdd("s", Fix(SecDiff * Rnd()), StartDate)
  Next

输出(德语区域设置):

15.09.2013 09:00:00 21.09.2013 15:00:01 540001
19.09.2013 18:49:56
18.09.2013 17:00:49
18.09.2013 23:55:40
17.09.2013 04:26:04
17.09.2013 06:17:32
20.09.2013 05:12:40
...