python字符串和time互转与时间的加减另加uuid
2017-04-18 14:19
225 查看
# -*-coding:utf-8 -*- __author__ = "ZJL" import uuid,time,datetime #uuid4产生32位随机字母加数字 print(str(uuid.uuid4()).replace("-","")) #uuid3产生基于名字的MD5散列值 print(str(uuid.uuid3(uuid.NAMESPACE_DNS,"username")).replace("-","")) #time转字符串 time_num = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print(time_num) #字符串转time t = time.strptime(time_num, '%Y-%m-%d %H:%M:%S') y,m,d,H,M,S = t[:6] print(t) print(datetime.datetime(y,m,d,H,M,S)) #时间的加减 now_time = datetime.datetime.now() #当前时间加半小时 yes_time = now_time + datetime.timedelta(hours=+0.5) #比较时间大小 if now_time>yes_time: print("ok") else: print("no") #当前时间减一天 # yes_time = now_time + datetime.timedelta(days=-1) yes_time_nyr = yes_time.strftime('%Y-%m-%d %H:%M:%S') print(yes_time_nyr)
结果:
import time, datetime
#一个月前
today1 = datetime.datetime.today()
astmonth = datetime.datetime(today1.year, (today1.month - 1), today1.day, today1.hour, today1.minute,today1.second)
相关文章推荐
- Python的time(时间戳与时间字符串互相转化)
- Python的time(时间戳与时间字符串互相转化)
- Python的time(时间戳与时间字符串互相转化)
- Python之time模块的时间戳、时间字符串格式化与转换(13位时间戳)
- python时间模块time,时间戳,结构化时间,字符串时间,相互转换,datetime
- 【python】详解time模块功能asctime、localtime、mktime、sleep、strptime、strftime、time等函数以及时间的加减运算
- 利用Python的time模块创建时间戳
- Python中时间戳与时间字符串互相转化
- python,时间加减,时间计算,时间格式化,时间提取汇总
- python 取出当前时间详解 time
- Python标准库02 时间与日期 (time, datetime包)
- python日期时间字符串转换成unix时间戳
- python日期时间字符串转换成unix时间戳
- [Dynamic Language] Python 日期时间格式化字符串
- python os.path模块/Python os.listdir/字符串处理/python 时间datetime.datetime
- 【整理】Python中字符串与时间的转换与计算
- 字符串转时间(time_t)
- Python的时间:秒和字符串之间的转换
- 关于Python中时间与字符串直接的转换
- js时间加减与时间对象与字符串之间的转换