how to generate date along with time randomly between a start date and end date using VB script. We have to take input from user
Example: startDate:15/09/2013 9:00:00 Enddate 21/09/2013 15:00:00
Output : Random date :17/09/2013 12:00:00
Add a random number of seconds less than the difference between the dates in seconds to the 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
output (german locale):
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
...
Related
I'm new to vb scripts please help me.
I'm generating Random year by appending last two digits randomly as shown bellow but i want to generate random date DDMMYYYY format between start date and end date provided by user.
Randomize
tmp = Int((92 - 70 + 1) * Rnd + 70)
tmp1= "01/05/19" & tmp
Use this as a starting point
Option Explicit
Function getRandomDate( startDate, endDate )
getRandomDate = DateAdd( _
"d" _
, Fix( DateDiff("d", startDate, endDate ) * Rnd ) _
, startDate _
)
End Function
Dim startDate, endDate
startDate = CDate("2016/03/10")
endDate = CDate("2017/09/30")
Randomize
Dim i
For i = 0 To 100
WScript.Echo getRandomDate( startDate, endDate )
Next
It just calculates the number of days between the start and end dates and selects a random in this range of days to add to the start date.
As an addendum to #mc-nd's answer
When using Rnd() to generate random numbers you sure to use Randomize to initialise the Random Number Generator using the System Time as a seed value or this happens.
Option Explicit
Function getRandomDate( startDate, endDate )
getRandomDate = DateAdd( _
"d" _
, Fix( DateDiff("d", startDate, endDate ) * Rnd ) _
, startDate _
)
End Function
Dim startDate, endDate
startDate = CDate("2016/03/10")
endDate = CDate("2017/09/30")
Dim i
For i = 0 To 10
WScript.Echo getRandomDate( startDate, endDate )
Next
1st Output:
15/04/2017
07/01/2017
02/02/2017
21/08/2016
28/08/2016
24/05/2017
17/03/2016
16/05/2017
16/06/2017
17/04/2017
04/04/2016
2nd Output:
15/04/2017
07/01/2017
02/02/2017
21/08/2016
28/08/2016
24/05/2017
17/03/2016
16/05/2017
16/06/2017
17/04/2017
04/04/2016
Add the Randomize statement to the code to see the difference;
Option Explicit
Function getRandomDate( startDate, endDate )
getRandomDate = DateAdd( _
"d" _
, Fix( DateDiff("d", startDate, endDate ) * Rnd ) _
, startDate _
)
End Function
Dim startDate, endDate
startDate = CDate("2016/03/10")
endDate = CDate("2017/09/30")
Dim i
'Call the random number generator with the system time as the seed.
Call Randomize()
For i = 0 To 10
WScript.Echo getRandomDate( startDate, endDate )
Next
1st Output:
14/11/2016
28/09/2016
26/05/2016
06/01/2017
22/09/2016
13/06/2016
12/11/2016
05/03/2017
05/05/2016
01/05/2016
03/02/2017
2nd Output:
08/03/2017
20/01/2017
18/04/2016
29/11/2016
16/08/2016
07/05/2016
06/10/2016
27/01/2017
22/07/2016
19/07/2016
21/09/2017
It's an easy thing to miss but makes a BIG difference.
Dim Date1 As Date, Date2 As Date
Date1 = 10/02/2017
Date2 = 10/06/2017
iDiff = DateDiff("d", Date1, Date2, vbMonday)
Dim RndDate As Date
Randomize
RndDate = DateAdd("d", Int((iDiff * Rnd) + 1), Date1)
I find simple way to generate random Date.
How can I output a date minus 3 hours? Example:
BookDate = 8/4/2014 9:00:00 PM
DesirebleDate = 8/4/2014 6:00:00 PM
Tried DesirebleDate = DateDiff("h", BookDate , -3) but it is outputing the amount of hours. Thanks.
DesirableDate = DateAdd("h", -3, BookDate)
Should set DesirableDate to 3 hours earlier than BookDate. DateDiff gives the difference between two dates, which is why you are getting the 3...
I want to convert date to number in oracle 11g .
The date is stored in (12/03/2013 12:23:00).
I want to convert this to Number(The date corresponding number value).
As in java we convert date to long .
I want the same to be done here .
Calendar c = Calendar.getInstance();
c.set(2013, 05, 23, 0, 0, 0);
Date date = c.getTime();
System.out.println("Date is " + date);
long longDate = date.getTime();
System.out.println("Date as long :" + longDate);
Date d = new Date(longDate);
System.out.println("Converted Date :" + d);*
The Output is :
**Date is Sun Jun 23 00:00:00 SGT 2013
Date as long :1371916800981
Converted Date :Sun Jun 23 00:00:00 SGT 2013**
Now I want to store value as 1371916800981
I am guessing that the long data type that you want is something like the number of seconds or milliseconds since 1970-01-01.
To get this requires just a bit of arithmetic:
select (to_date(s1, 'MM/DD/YYYY HH24:MI:SS') -
to_date('1970-01-01', 'YYYY-MM-DD')
) *24*60*60
from (select '12/03/2013 12:23:00' as s1 from dual
) t
I note that your result is using the current time stamp. This might include milliseconds which this constant date format doesn't include.
You can achieve this with a help of RAW data type:
SELECT
s1,
to_number(TO_CHAR(s1,'YYYYMMDDHH24MISS')) as s2,
utl_raw.cast_to_raw(s1) as d1,
utl_raw.cast_to_raw(s2) as d2,
round(utl_raw.cast_to_number(utl_raw.cast_to_raw(s1)),20) as s1,
round(utl_raw.cast_to_number(utl_raw.cast_to_raw(s2)),20) as s2,
utl_raw.cast_to_number(utl_raw.cast_to_raw(s1)) - utl_raw.cast_to_number(utl_raw.cast_to_raw(s2)) as s1_s2_diff
FROM
( select
sysdate as s1,
sysdate-1/24/60/60 as s2
from dual );
Output:
S1 S2
------------------- --------------
2014.03.16 11:14:16 20140316111416
D1 D2
-------------------------------------- --------------------------------------
323031342E30332E31362031313A31343A3136 323031342E30332E31362031313A31343A3135
S1_1 S2_1
-------------------------------------- --------------------------------------
-53524955535055524769525243,5249435247 -53524955535055524769525243,5249435248
SDIFF
------------
0,0000000001
I want to get the previous month total day count
Code
Dim period as string
period = '01/2011'
totdays = DateDiff("d", txtPeriod, DateAdd("m", 1, txtPeriod))
'this will give the totaldays of the month...
But i want to get total days of the previous month
User will type current month only, but code should validate previous month
Expected Output
If period = '02/2011' means then it should display 31 days 'January
If period = '03/2011' means then it should display 28 days 'February
How to do this...
Any Help
This worked fine for me. Also, why do you have a variable period, but use txtPeriod in your calculations?
Dim dt As Date
Dim DaysInMonth As Integer
dt = CDate(txtPeriod.Text)
dt = DateAdd("m", -1, dt)
DaysInMonth = DateDiff("d", dt, DateAdd("m", 1, dt))
USING VB 6.0
How to select previous row value?
Am Selecting a Date between fromdate and todate using date picker
Code
Dim stdate, endate as string
stdate = Fromdate
endate = todate
Example:
fromdate: 01-01-2009
todate: 01-06-2009
Data’s will display fromdate to todate.
I want to select previous date means previous row value.
How to select previous row value?
We cannot give
stdate = - fromdate
endate = - todate
It will display a data’s between 31-01-2008 to 31-05-2009
We cannot give
Stdate = <fromdate
endate = < todate
It will display a data’s before 01-01-2009 and also we cannot use > in between condition
Stdate = fromdate (Here how can I give “from previous row value of the fromdate”)
endate = todate (here how can I give “to previous row value of the todate”)
Example:
id, date, name
01, 02-01-2009, raja
01, 04-01-2009, raja
02, 04-01-2009, ravi
so on.....
01, 28-05-2009, raja
01, 31-05-2009, raja
so on...
am selecting a date stdate = 04-01-2009, endate= 31-05-2009
Output shoud display like this -
01, 02-01-2009, raja
01, 04-01-2009, raja
02, 04-01-2009, ravi
.......
01, 28-05-2009, raja
It should dipslay one record Before ssdate and endate.
How to select previous row value?
Need Help in VB 6 code.
I do not completely understand what you are doing, but if you have a recordset object that is sorted on date you need to need something like
If (recordset.BOF = False) Then
recordset.MovePrevious
End If
If you are asking how to get a recordset that contains one record before the start date you are going to have to expand your query to try to include at least on record before the fromdate, find the first record with the fromdate, then use the MovePrevious method above.
If you are just asking how to get a date prior to a given date then use the DateAdd method.
example - the below code will subtract 1 day from 2/1/2009 and put 1/31/2009 into the valriable.
dtPreviousDate = DateAdd("D", CDate("02-01-2009"), -1)