内建模块 datetime使用

#-*- coding:utf-8  -*-
from  datetime  import  datetime

#获取当前日期和时间
now =datetime.now()
print now
# 2017-08-10 16:41:57.437000

print  (type(now))
# <type 'datetime.datetime'>

# 获取指定日期和时间
dt=datetime(2015,4,19,12,20)   #用指定日期时间创建datetime
print(dt)
# 2015-04-19 12:20:00
# print dt.timestamp()


t=1429417200.0
print(datetime.fromtimestamp(t))   #本地时间
# 2015-04-19 12:20:00
print(datetime.utcfromtimestamp(t))   #UTC时间
# 2015-04-19 04:20:00

#把str转换为datetime
cday=datetime.strptime('2015-6-1  18:19:59','%Y-%m-%d  %H:%M:%S')

print  cday
# 2015-06-01 18:19:59

#datetime  转为str
now=datetime.now()
print(now.strftime('%a,%b %d %H:%M'))
# Thu,Aug 10 17:38

  

posted @ 2017-08-11 15:34  记忆残留  阅读(184)  评论(0编辑  收藏  举报