1 获取北京时间
import time
import requests
def getBeijinTime():
try:
hea = {'User-Agent': 'Mozilla/5.0'}
url = r'http://time1909.beijing-time.org/time.asp'
r = requests.get(url=url, headers=hea)
if r.status_code == 200:
result = r.text
data = result.split(";")
year = data[1][len("nyear") + 3: len(data[1])]
month = data[2][len("nmonth") + 3: len(data[2])]
day = data[3][len("nday") + 3: len(data[3])]
hrs = data[5][len("nhrs") + 3: len(data[5])]
minute = data[6][len("nmin") + 3: len(data[6])]
sec = data[7][len("nsec") + 3: len(data[7])]
beijinTimeStr = "{}-{}-{}".format(year, month, day)
beijinTimeStr_all = "{}-{}-{}_{}-{}-{}" .format(year, month, day, hrs, minute, sec)
beijinTimeList = []
beijinTimeList.append(beijinTimeStr)
beijinTimeList.append(beijinTimeStr_all)
return beijinTimeList
except:
return ['9999-99-99', '9999-99-99_9-9-9']
if __name__ == '__main__':
print(getBeijinTime())
2 时间计算
>>>import datetime
>>> d1 = datetime.datetime.now()
>>> d1.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-02 17:11:07'
>>> d2 = d1 + datetime.timedelta(seconds=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-02 17:11:17'
>>> d2 = d1 + datetime.timedelta(minutes=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-02 17:21:07'
>>> d2 = d1 + datetime.timedelta(hours=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-03 03:11:07'
>>> d2 = d1 + datetime.timedelta(days=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-12 17:11:07'
>>> d2 = d1 - datetime.timedelta(seconds=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-02 17:11:57'
>>> d2 = d1 - datetime.timedelta(minutes=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-02 17:01:07'
>>> d2 = d1 - datetime.timedelta(hours=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-09-02 07:11:07'
>>> d2 = d1 - datetime.timedelta(days=10)
>>> d2.strftime("%Y-%m-%d %H:%M:%S")
'2013-08-23 17:11:07'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)