Python随笔之常用模块-time&datetime模块

1.time模块

  • time.sleep() 休眠,参数为秒
  • time.time() 从1970-01-01 开始到现在的毫秒数,当前系统的时间戳
  • time.ctime() 返回当前时间的字符串表示
  • time.ctime(time.time()) 将时间戳转换为字符串表示
  • time.gmtime() 将时间戳转换为struct_time格式
  • time.localtime() 将时间戳转换为struct_time格式,本地时间
  • time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()) 将struct_time格式转换为字符串
  • time.strptime('2019-02-24', '%Y-%m-%d') 将字符串转换为struct_time格式
import time
print(time.time())
print(time.ctime())
print(time.ctime(time.time() - 86640))
print(time.gmtime())
print(time.gmtime(time.time() - 86640))
print(time.localtime())
print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()))
print(time.strptime('2019-02-24', '%Y-%m-%d'))

>>> 1550995222.433002
>>> Sun Feb 24 16:00:22 2019
>>> Sat Feb 23 15:56:22 2019
>>> time.struct_time(tm_year=2019, tm_mon=2, tm_mday=24, tm_hour=8, tm_min=0, tm_sec=22, tm_wday=6, tm_yday=55, tm_isdst=0)
>>> time.struct_time(tm_year=2019, tm_mon=2, tm_mday=23, tm_hour=7, tm_min=56, tm_sec=22, tm_wday=5, tm_yday=54, tm_isdst=0)
>>> time.struct_time(tm_year=2019, tm_mon=2, tm_mday=24, tm_hour=16, tm_min=0, tm_sec=22, tm_wday=6, tm_yday=55, tm_isdst=0)
>>> 2019-02-24 08:00:22
>>> time.struct_time(tm_year=2019, tm_mon=2, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=55, tm_isdst=-1)
  • datetime.date.today() 输出2019-02-24格式
  • datetime.date.fromtimestamp(time.time()) 输出2019-02-24格式
  • datetime.datetime.now() 返回当前时间
  • datetime.datetime.now().timetuple() 返回struct_time格式
  • datetime.datetime.now().replace(1996,9,20) 替换年月日
  • datetime.datetime.now() + datetime.timedelta(day=10) 比现在的时间+10天
posted @ 2019-02-24 16:04  新科鲁兹  阅读(113)  评论(0编辑  收藏  举报