python时间和时间戳之间的相互转换,python时间相互转换,时间戳与时间的转换经常要
python时间和时间戳之间的相互转换,python时间相互转换,时间戳与时间的转换经常要
时间戳与时间的转换
经常要操作时间,可就是记不住这个转换过程,记录一下:
时间戳与时间之间的转换,需要一个中间过程,即将先将时间或时间戳先转为时间元组!
1、时间转时间戳:
>>> import datetime>>> s = datetime.datetime(2012,6,22)>>> time.mktime(s.timetuple())1340294400.0
2、时间戳转时间:
>>> import time>>> timeTuple = time.localtime(1340294400.0)>>> time.strftime('%Y-%m-%d',timeTuple)'2011-06-22'
3、时间元组:共有9个元素,struct_time方式表示,如:
>>> time.localtime()time.struct_time(tm_year=2012, tm_mon=6, tm_mday=22, tm_hour=12, tm_min=5, tm_sec=49, tm_wday=4, tm_yday=174, tm_isdst=0)
1)time.localtime([secs]):将一个时间戳转换为当前时区的struct_time,参数未提供则以当前时间为准。
2)time.gmtime([secs]):和localtime()方法类似,将一个时间戳转换为UTC时区(0时区)的struct_time。
相关内容
- python filter使用示例,pythonfilter示例,列表用得比较多,
- python使用ConfigParser库读取配置文件,pythonconfigparser,温习
- python 检测关键词在百度的排名,python关键词,python检测
- python urllib 使用总结,pythonurllib,import urlli
- python使用sgmllib库 SGMLParser解析页面中的链接地址,sgm
- python使用ftplib做ftp操作,pythonftplib,ftplib是 Pyth
- webpy的LightTPD配置,webpyLightTPD配置,在生产环境下推荐在
- webpy 性能调优记录页面执行时间,webpy调优,记录网站执
- python使用urllib2上传文件代码,pythonurllib2,import urlli
- python中文字符编码decode,encode以及乱码解决总结,pyt
评论关闭