datetime在python中比较常用,主要用来处理时间日期,使用前先倒入datetime模块。下面总结下本人想到的几个常用功能。
>>> print datetime.datetime.now()
2015-07-17 16:39:15.712000
>>> print type(datetime.datetime.now())
<type ‘datetime.datetime‘>
返回的datetime时间格式。
>>> print datetime.datetime.now().date()
2015-07-17
>>> print type(datetime.datetime.now().date())
<type ‘datetime.date‘>
>>> datetime.datetime.now().timetuple()
time.struct_time(tm_year=2015, tm_mon=7, tm_mday=17, tm_hour=16, tm_min=51, tm_sec=26, tm_wday=4, tm_yday=198, tm_isdst=-1)
>>> datetime.datetime.now().timetuple().tm_mday
17
使用datetime.timedelta这个方法来前后移动时间,可以用的参数有weeks,days,hours,minutes,seconds,microseconds。
>>> print datetime.datetime.now() + datetime.timedelta(days=1)
2015-07-18 16:49:48.574000
>>> print datetime.datetime.now() + datetime.timedelta(hours=1)
2015-07-17 17:49:57.122000
>>> print datetime.datetime.now() + datetime.timedelta(minutes=-30)
2015-07-17 16:20:08.619000
上个月最后一天
>>> print datetime.date(day=1,month=datetime.date.today().month,year=datetime.date.today().year) - datetime.timedelta(days=1)
2015-06-30
>>> (datetime.datetime.now() - datetime.datetime.utcnow()).total_seconds()
28800.0
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
‘2015-07-17 16:58:46‘
>>> datetime.datetime.strptime("2015-07-17 16:58:46","%Y-%m-%d %H:%M:%S")
datetime.datetime(2015, 7, 17, 16, 58, 46)
>>> print datetime.datetime.strptime("2015-07-17 16:58:46","%Y-%m-%d %H:%M:%S")
2015-07-17 16:58:46
>>> print type(datetime.datetime.strptime("2015-07-17 16:58:46","%Y-%m-%d %H:%M:%S"))
<type ‘datetime.datetime‘>
>>> import time
>>> now=datetime.datetime.now()
>>> time.mktime(now.timetuple())
1437123812.0
>>> datetime.datetime.fromtimestamp(1437123812.0)
datetime.datetime(2015, 7, 17, 17, 3, 32)
>>> print datetime.datetime.fromtimestamp(1437123812.0)
2015-07-17 17:03:32
原文:http://www.cnblogs.com/liuchunxiao83/p/6403739.html