当前位置:首页 > 日记本 > 正文内容

python随机时间范围/随机时间段

zhangchap2年前 (2022-11-23)日记本366
import datetime,random
# 昨天时间
today = datetime.date.today()
# 今天时间
yesterday = today - datetime.timedelta(days=1)
yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1
# 随机时间戳
timeStamp = random.randint(yesterday_start_time,yesterday_end_time)

print(timeStamp)

其实就是先把当天的开始、结束时间戳找出来,然后随机这个时间段的时间戳,就把这个时间段的随机时间拿到手了

分享给朋友:

相关文章

火狐添加自定义搜索引擎

直接网址搜索自定义添加:https://mycroftproject.com/...

Nginx+PHP,PHP如何优化配置?

具体修改FPM配置文件参数: 若你的php日志出现: WARNING: [pool www] seems busy (you may need to increase pm.sta...

python使用mongodb数据库

from pymongo import MongoClient,collection class KSpdier(Thread):   ...

python 随机生成时间戳写入txt文件/运行sql语句

import time from random import randint with open('time.txt', ...

python 发布文章 随机分类(choice)

from random import choice catid = choice([5,6]) #choice 函数从列表中随机提取...

Python 正则表达式 带分组的替换 \g

import re re.sub(r'([^a-z]*)[a-z]([^a-z]*)', '\g<1>\g<2>',wor...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。