每天CookBook之Python-055

  • 将字符串转换为实践
from datetime import datetime

text = '2012-09-20'
y = datetime.strptime(text, '%Y-%m-%d')
z = datetime.now()
diff = z - y
print(diff)
print(z)

nice_z = datetime.strftime(z, '%A, %B %d, %Y')
print(nice_z)


def parse_ymd(s):
    year_s, mon_s, day_s = s.split('-')
    return datetime(int(year_s), int(mon_s), int(day_s))


print(parse_ymd(text))

Out

1399 days, 23:22:34.005031
2016-07-20 23:22:34.005031
Wednesday, July 20, 2016
2012-09-20 00:00:00
posted @ 2016-07-21 22:37  4Thing  阅读(117)  评论(0)    收藏  举报