python之模块datetime 常见操作

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之模块datetime
#http://blog.sina.com.cn/s/blog_6c3748830100ypt9.html
#http://blog.sina.com.cn/s/blog_dd49240d0102w0y6.html import datetime
'''
NAME:
datetime - Fast implementation of the datetime type.
'''
#help(datetime),datetime模块下的几个类:
'''
datetime.date
.today()
datetime.time
datetime.datetime
.now()
.timedelta(days=1)
datetime.timedelta
datetime.tzinfo
''' import datetime
import time #今天日期
today=datetime.date.today()
print today#2015-09-05 #日期对象;date(year, month, day) --> date object
s=datetime.date(2015,9,1)
print s,type(s)#2015-09-01 #获取当前系统时间
date=datetime.datetime.now()
print date#2015-09-05 16:34:41.460000 #返回昨天的日期
def getYesterday():
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
return yesterday #获取当前年份,月份等,是一个字符串
def getYear():
year=str(datetime.date.today())[0:4]
month=str(datetime.date.today())[5:7]
return year,month print 'getYear:',getYear() #返回当前时间的时间戳
s=time.time()
print s #计算2个日期的天数差
import datetime
d1=datetime.datetime(2015,9,1)
d2=datetime.datetime(2015,9,5)
print d2-d1#4 days, 0:00:00
s=(d2-d1).days
print s#4天 #计算当前时间向后10小时的时间
import datetime
d1=datetime.datetime.now()
d2=d1+datetime.timedelta(hours=10)#注意hours,是有s的
print d2.ctime()#Sun Sep 6 02:53:21 2015 #计算上个月的月份
import time
last_month = time.localtime()[1]-1 or 12
print last_month
上一篇:RabbitMQ基础使用之集群构建


下一篇:HDU 1867 A + B for you again ----KMP