I need to append random hours to date field. For this I used following code:
我需要随机添加小时数字段。为此,我使用以下代码:
datetime.utcnow().date() + relativedelta(hours=random.randint(0,23))
This returned response:
这回复了回复:
datetime.date(2018, 7, 5)
Above response is not reproducible. Wondering if using random.randint() is reliable. Please let me know what caused this to occur or what other solution I can use for this problem.
以上反应不可重复。想知道使用random.randint()是否可靠。请让我知道导致此问题的原因或我可以使用的其他解决方案来解决此问题。
1 个解决方案
#1
1
This happens exactly when random.randint(0,23)
returns 0
. In that case, you get a final result of type datetime.date
instead of datetime.datetime
, because the delta is basically zero. So random.randint()
is reliable, but sometimes it does return 0
, as expected.
当random.randint(0,23)返回0时,就会发生这种情况。在这种情况下,您将得到datetime.date类型的最终结果,而不是datetime.datetime,因为delta基本上为零。所以random.randint()是可靠的,但有时它会像预期的那样返回0。
#1
1
This happens exactly when random.randint(0,23)
returns 0
. In that case, you get a final result of type datetime.date
instead of datetime.datetime
, because the delta is basically zero. So random.randint()
is reliable, but sometimes it does return 0
, as expected.
当random.randint(0,23)返回0时,就会发生这种情况。在这种情况下,您将得到datetime.date类型的最终结果,而不是datetime.datetime,因为delta基本上为零。所以random.randint()是可靠的,但有时它会像预期的那样返回0。