5、时间日期:
1)
import time
localtime = time.asctime( time.localtime(time.time()) )
print "本地时间为 :", localtime
以上实例输出结果:
本地时间为 : Thu Apr 7 10:05:21 2016
local_time = time.localtime(time.time())
create_time = time.strftime('%Y-%m-%d %H:%M:%S', local_time) # 采集创建时间
print(create_time)
2021-10-18 16:00:42
21、判断字符串时间的合法性
import datetime
def verify_date_str_lawyer(datetime_str):
try:
datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
return True
except ValueError:
return False
print(verify_date_str_lawyer('2003-12-23 23:59:59'))
print(verify_date_str_lawyer('2003-06-30 12:61:52'))
56、# python学习-将一串数字形式的时间转换为标准时间格式
import time
tupTime = time.localtime(1566366555)#秒时间戳
stadardTime = time.strftime("%Y-%m-%d %H:%M:%S", tupTime)
print(stadardTime)
2019-08-21 13:49:15
65、'2021-08-10'日期时间转为数字时间戳(https://www.cnblogs.com/jfl-xx/p/8024596.html)
字符类型的时间
tss1 = '2021-08-10 00:00:00'
转为时间数组
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
转为时间戳
timeStamp = int(time.mktime(timeArray))
print(timeStamp) # 1628524800
69、当前日期时间
import datetime
a=str(datetime.datetime.now())[0:19]
print(a)
6、利用Python获取某个日期(形如xxxx-xx-xx)之前或之后多少天的日期:
import datetime
def get_day(date, step=0):
l = date.split("-")
y = int(l[0])
m = int(l[1])
d = int(l[2])
old_date = datetime.datetime(y, m, d)
new_date = (old_date + datetime.timedelta(days=step)).strftime('%Y-%m-%d')
print(new_date)
return new_date
20、日期转换
import datetime
a = '2018-10-03'
b = datetime.datetime.strptime(a, '%Y%m%d').strftime('%Y-%m-%d')
28、python获取当天日期
- datetime.datetime.now().strftime('%Y%m%d')
- today = datetime.date.today()
32、Python3 实例--Python 获取昨天日期
https://blog.csdn.net/qq_33410995/article/details/104294093
today = datetime.date.today()
oneday = datetime.timedelta(days=1)
yesterday = today - oneday
33、Python中GMT时间格式转化(https://blog.csdn.net/mz02230909mz/article/details/107776057)
https://blog.csdn.net/weixin_43919753/article/details/119349385
from datetime import datetime
GMT_FORMAT = ‘%a, %d %b %Y %H:%M:%S GMT+0800 (CST)’
print(datetime.utcnow().strftime(GMT_FORMAT))
output:
Mon, 12 Nov 2018 08:53:51 GMT+0800 (CST)
dd = “Fri Nov 09 2018 14:41:35 GMT+0800 (CST)”
GMT_FORMAT = ‘%a %b %d %Y %H:%M:%S GMT+0800 (CST)’
print(datetime.strptime(dd, GMT_FORMAT))
output:
2018-11-09 14:41:35
34、将格式化字符串转换为时间戳(https://www.cnblogs.com/strivepy/p/10436213.html)
str_time = '2019-02-26 13:04:41'
print( time.mktime(time.strptime(str_time, '%Y-%m-%d %H:%M:%S')))
35、