日期转时间戳
def unix_time(self, dt):
'''
日期转时间戳
:param dt:
:return:
'''
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
timestamp = int(time.mktime(timeArray))
return timestamp
自定义时间格式转换
def dateFormatString(self, timestamp, timeType):
"""
时间用指定格式显示,比如 年-月-日 转 年/月/日
"""
timeArray = time.strptime(timestamp, timeType)
customTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
return customTime
时间戳转日期
def custom_time(self, timestamp, timeTypr):
time_local = time.localtime(timestamp)
dt = time.strftime(timeTypr, time_local)
return dt
判断是否在n天以内
def isRightDayDate(self, pubdate, rightday):
'''
距离现在多少天
传入的数据格式必须是'%Y-%m-%d %H:%M:%S'
:return:
'''
timeNow = datetime.datetime.now()
pdate = datetime.datetime.strptime(pubdate, '%Y-%m-%d %H:%M:%S')
if abs((timeNow - pdate).days) < rightday :
return True
else:
return False