gpt4 book ai didi

python - 如何在 Python 中生成 future 5 天内的随机时间戳?

转载 作者:行者123 更新时间:2023-11-28 20:59:07 30 4
gpt4 key购买 nike

我想在接下来的 5 天中的任何一天的上午 8 点到下午 3 点之间随机获取 Python 时间。我试过在 date.now() 上使用 timedelta,但我的结果从来都不是特别随机的(我反复得到相同的日期,这让我认为 timedelta 与随机)

最佳答案

today_random_time_between_8_and_3 = datetime.datetime.now().replace(hour=randint(8,14),minute=randint(0,59))
random_day = today_random_time_between_8_and_3 + datetime.timedelta(days=random.randint(0,5))
print(random_day)

这会在上午 8 点到下午 2 点之间随机选择一个小时...然后在 0 到 59 之间随机添加分钟

最后它只是选择一个介于 1 和 4 之间的随机数并将那么多天添加到今天的随机时间

关于python - 如何在 Python 中生成 future 5 天内的随机时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277935/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com