Goodspeed

导航

Python中时间的处理之——date和time篇

#! /usr/bin/python
#
coding=utf-8

from datetime import datetime,date,time

"""
date类型顾名思义就是只表示日期,而time只表示time
"""
today = date.today()

attrs = [
("year",""),( 'month',""),( 'day',"")
]
for k,v in attrs:
"today.%s = %s #%s" % (k,getattr(today, k),v)

#星期几。同datetime规则一样
today.isoweekday()
today.weekday()

#返回一个time结构,当然也就没有时间信息
today.timetuple()

#修改。同datetime规则一样
today.replace(month=1)

#转成字符串,不可以转回
today.strftime("%Y-%m-%d")

now = time(12,13,14)
attrs = [
("hour","小时"),( 'minute',""),( 'second',""),("microsecond","毫秒")
]
for k,v in attrs:
"now.%s = %s #%s" % (k,getattr(now, k),v)

#修改。同datetime规则一样
now.replace(minute=1)
#转成字符串,不可以转回
now.strftime("%H:%M:%S")

#date和time合并成datetime
print datetime.combine(today, now)



posted on 2011-11-07 10:28  Goodspeed  阅读(3321)  评论(0编辑  收藏  举报