【发布时间】:2021-10-07 10:29:59
【问题描述】:
How would I generate a random date that has to be between two other given dates?
The function's signature should be something like this:
random_date("1/1/2008 1:30 PM", "1/1/2009 4:50 AM", 0.34)
^ ^ ^
date generated has date generated has a random number
to be after this to be before this
and would return a date such as: 2/4/2008 7:20 PM
【问题讨论】:
-
The way the question is presented at the moment it isn't clear whether or not you only want the date or the time to be random. Your example suggests that you are looking for a time. If it has to be in between the two dates you may want to modify the answers given so far to suit your needs and exclude the end and start time. Lastly, in most answers, such as the accepted one, the code outputs a datetime exclusive the endtime due to truncating to int. To generate a time that may include the end in the answer change the code to
ptime = stime + prop * (etime - stime) + 0.5 -
Yes, probably the question was about interpolation, and everybody who finds it wants a random date :)