¿Cómo generar una fecha y hora aleatorias entre dos fechas?


No pase por alto la parte de 'fecha Y HORA'.

Author: potashin, 2010-04-21

8 answers

Time.at((date2.to_f - date1.to_f)*rand + date1.to_f)

Obtendrá un objeto time que se encuentra entre dos datetimes dados.

 57
Author: Evgeny Shadchnev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-21 14:42:17

Debería ser capaz de generar fechas/horas aleatorias dentro de un cierto rango.

now = Time.now
a_day_ago = now - 60 * 60 * 24

random_time = rand(a_day_ago..now)

# with activesupport required
up_to_a_year_ago = rand(1.year.ago..Time.now)

Sus entradas deben ser la clase Time, o convertirse en una.

También puedes hacer un rango de tiempo en epoch time y luego usar Time#at.

now = Time.now.to_i
minute_ago = (Time.now - 60).to_i
Time.at(rand(minute_ago..now))
 33
Author: Jack Chu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-17 17:13:34

Utilice el método rand().

require 'time'
t1 = Time.parse("2015-11-16 14:40:34")
t2 = Time.parse("2015-11-20 16:20:23")
puts rand(t1..t2)
 5
Author: SharnieIvery,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-17 01:48:47

Use time.to_i() (consulte la clase Time) para convertir sus fechas en enteros, aleatorizar entre esos dos valores, luego reconvertir a hora y fecha con Time.at().

 4
Author: Federico klez Culloca,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-21 14:39:28

No se sobre ruby pero ¿por qué no generas un entero y lo usas junto con una marca de tiempo? A continuación, simplemente convierta la marca de tiempo al formato deseado.

 2
Author: Max Leske,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-21 14:35:51

Si conoce el punto medio y el rango de desplazamiento a cada lado del punto medio, puede usar mi gema ish: https://github.com/spilliton/ish

mid_date.ish(:offset => 60.days)
 1
Author: spilliton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-01 16:03:07
Time.at(rand(Time.parse('some date').to_i..Time.now.to_i))
 1
Author: alebian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-27 18:04:13

Calcule la diferencia en, por ejemplo, minutos entre las dos fechas, genere un número aleatorio entre 0 y ese número, y agregue ese número de minutos a la primera fecha.

 0
Author: Maarten Ureel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-21 14:37:59