你的分享就是我们的动力 ---﹥

Python复习笔记—日期时间

时间:2012-07-24 13:58来源:www.chengxuyuans.com 点击:

1. 获取timestamp,忽略毫秒

?

?

from datetime import datetime

def get_timestamp(atime) :
    return (atime - datetime(1970,1,1)).total_seconds()

2. 仅保留日期

?

>>> dateonly = datetime.today().replace(hour=0, minute=0, second=0,microsecond=0)
>>> str(dateonly)
'2012-07-11 00:00:00'

3. 日期加减

?

def next_day(atime) :
	return datetime.utcfromtimestamp(get_timestamp(now) + 86400L)

?新的版本有timedelta

?

from datetime import timedelta

def next_day(atime) :
	return atime + timedelta(days=1)

转载注明地址:http://www.chengxuyuans.com/Python/42315.html

推荐文章