[Gambas-user] Using Date datatype for FOR-NEXT
gambas at ...1...
Mon Jun 18 08:39:42 CEST 2007
On lundi 18 juin 2007, Rolf-Werner Eilert wrote:
> I did this at another place in the program, but with dates which were
> produced by the program.
> Here I was looking for a way to produce random dates at any point of
> start to any point of end (when the user clicks "one day ahead" or "one
> day back").
> Meanwhile I built a small thing that can calculate the dates itself,
> using a lot of increasing, decreasing and IFs and stuff. But Ron's idea
> is good, isn't it?
The simplest way to do arithmetic on dates is converting them to a Float
number: the integer part is the number of days since the Epoch, and the
fractional part is the number of hours, minutes, seconds & microsecond since
the beginning of the day (0.5 is 12 hours, 0.25 is 6 hours, and so on...)
So, to produce random dates between two dates, you can do that:
MyRandomDate = CDate(Int(Rnd(CFloat(MyStartDate), CFloat(MyEndDate + 1))))
The MyEndDate + 1 is needed if you want MyEndDate to be in the random
More information about the User