http://blog.csdn.net/jgood/article/category/554799
这个python讲的非常好!
http://blog.sina.com.cn/s/blog_a15aa569010183s8.html这个也可以
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致。相比于time模块,datetime模块的接口则更直观、更容易调用。今天就来讲讲datetime模块。
- datetime.date:表示日期的类。常用的属性有year, month, day;
- datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
- datetime.datetime:表示日期时间。
- datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
- datetime.tzinfo:与时区有关的相关信息。(这里不详细充分讨论该类,感兴趣的童鞋可以参考python手册)
date类
- year的范围是[MINYEAR, MAXYEAR],即[1, 9999];
- month的范围是[1, 12]。(月份是从1开始的,不是从0开始的~_~);
- day的最大值根据给定的year, month参数来决定。例如闰年2月份有29天;
- date.max、date.min:date对象所能表示的最大、最小日期;
- date.resolution:date对象表示日期的最小单位。这里是天。
- date.today():返回一个表示当前本地日期的date对象;
- date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象;
- datetime.fromordinal(ordinal):将Gregorian日历时间转换为date对象;(Gregorian Calendar :一种日历表示方法,类似于我国的农历,西方国家使用比较多,此处不详细展开讨论。)
-
from
datetime import* -
import
time -
-
print
'date.max:' , date.max -
print
'date.min:' , date.min -
print
'date.today():' , date.today() -
print
'date.fromtimestamp():' , date.fromtimestamp(time.time()) -
-
#
# ---- 结果 ---- -
#
date.max: 9999-12-31 -
#
date.min: 0001-01-01 -
#
date.today(): 2010-04-06 -
#
date.fromtimestamp(): 2010-04-06
-
from
datetime import* -
import
time -
-
print
'date.max:', date.max -
print
'date.min:', date.min -
print
'date.today():', date.today() -
print
'date.fromtimestamp():', date.fromtimestamp(time.time()) -
-
#
# ---- 结果 ---- -
#
date.max: 9999-12-31 -
#
date.min: 0001-01-01 -
#
date.today(): 2010-04-06 -
#
date.fromtimestamp(): 2010-04-06
- date.year、date.month、date.day:年、月、日;
- date.replace(year, month, day):生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
- date.timetuple():返回日期对应的time.struct_time对象;
- date.toordinal():返回日期对应的Gregorian Calendar日期;
- date.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
- data.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
- date.isocalendar():返回格式如(year,month,day)的元组;
- date.isoformat():返回格式如'YYYY-MM-DD’的字符串;
- date.strftime(fmt):自定义格式化字符串。在下面详细讲解。
-
now
= 2010 ,date( 04 , 06 ) -
tomorrow
= now.replace(day = 07 ) -
print
'now:' , now, ',tomorrow:' ,tomorrow -
print
'timetuple():' , now.timetuple() -
print
'weekday():' , now.weekday() -
print
'isoweekday():' , now.isoweekday() -
print
'isocalendar():' , now.isocalendar() -
print
'isoformat():' , now.isoformat() -
-
#
# ---- 结果 ---- -
#
now: 2010-04-06 , tomorrow: 2010-04-07 -
#
timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1) -
#
weekday(): 1 -
#
isoweekday(): 2 -
#
isocalendar(): (2010, 14, 2) -
#
isoformat(): 2010-04-06
-
now
= 2010,date( 04, 06) -
tomorrow
= now.replace(day = 07) -
print
'now:', now, ',tomorrow:' ,tomorrow -
print
'timetuple():', now.timetuple() -
print
'weekday():', now.weekday() -
print
'isoweekday():', now.isoweekday() -
print
'isocalendar():', now.isocalendar() -
print
'isoformat():', now.isoformat() -
-
#
# ---- 结果 ---- -
#
now: 2010-04-06 , tomorrow: 2010-04-07 -
#
timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1) -
#
weekday(): 1 -
#
isoweekday(): 2 -
#
isocalendar(): (2010, 14, 2) -
#
isoformat(): 2010-04-06
- date2 = date1 + timedelta
# 日期加上一个间隔,返回一个新的日期对象(timedelta将在下面介绍,表示时间间隔) - date2 = date1 - timedelta
# 日期隔去间隔,返回一个新的日期对象 - timedelta = date1 - date2
# 两个日期相减,返回一个时间间隔对象 - date1 < date2
# 两个日期进行比较
-
now
= date.today() -
tomorrow
= now.replace(day = 7 ) -
delta
= tomorrow - now -
print
'now:' , now, 'tomorrow:' ,tomorrow -
print
'timedelta:' , delta -
print
now + delta -
print
tomorrow > now -
-
#
# ---- 结果 ---- -
#
now: 2010-04-06 tomorrow: 2010-04-07 -
#
timedelta: 1 day, 0:00:00 -
#
2010-04-07 -
#
True
-
now
= date.today() -
tomorrow
= now.replace(day = 7) -
delta
= tomorrow - now -
print
'now:', now, 'tomorrow:' ,tomorrow -
print
'timedelta:', delta -
print
now + delta -
print
tomorrow > now -
-
#
# ---- 结果 ---- -
#
now: 2010-04-06 tomorrow: 2010-04-07 -
#
timedelta: 1 day, 0:00:00 -
#
2010-04-07 -
#
True
Time类
- time.min、time.max:time类所能表示的最小、最大时间。其中,time.min = time(0, 0, 0, 0), time.max = time(23, 59, 59, 999999);
- time.resolution:时间的最小单位,这里是1微秒;
- time.hour、time.minute、time.second、time.microsecond:时、分、秒、微秒;
- time.tzinfo:时区信息;
- time.replace([ hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] ):创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性(原有对象仍保持不变);
- time.isoformat():返回型如"HH:MM:SS"格式的字符串表示;
- time.strftime(fmt):返回自定义格式化字符串。在下面详细介绍;
使用例子:
-
from
datetime import* -
tm
= time(23 , 46 , 10 ) -
print
'tm:' , tm -
print
'hour: %d, minute: %d, second: %d, microsecond: %d' \ -
% (tm.hour, tm.minute, tm.second, tm.microsecond) -
tm1
= tm.replace(hour = 20 ) -
print
'tm1:' , tm1 -
print
'isoformat():' , tm.isoformat() -
-
#
# ---- 结果 ---- -
#
tm: 23:46:10 -
#
hour: 23, minute: 46, second: 10, microsecond: 0 -
#
tm1: 20:46:10 -
#
isoformat(): 23:46:10
-
from
datetime import* -
tm
= time(23, 46, 10) -
print
'tm:', tm -
print
'hour: %d, minute: %d, second: %d, microsecond: %d' \ -
% (tm.hour, tm.minute, tm.second, tm.microsecond) -
tm1
= tm.replace(hour = 20) -
print
'tm1:', tm1 -
print
'isoformat():', tm.isoformat() -
-
#
# ---- 结果 ---- -
#
tm: 23:46:10 -
#
hour: 23, minute: 46, second: 10, microsecond: 0 -
#
tm1: 20:46:10 -
#
isoformat(): 23:46:10
datetime类
- datetime.min、datetime.max:datetime所能表示的最小值与最大值;
- datetime.resolution:datetime最小单位;
- datetime.today():返回一个表示当前本地时间的datetime对象;
- datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
- datetime.utcnow():返回一个当前utc时间的datetime对象;
- datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;
- datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;
- datetime.combine(date, time):根据date和time,创建一个datetime对象;
- datetime.strptime(date_string, format):将格式字符串转换为datetime对象;
使用例子:
-
from
datetime import* -
import
time -
-
print
'datetime.max:' , datetime.max -
print
'datetime.min:' , datetime.min -
print
'datetime.resolution:' , datetime.resolution -
print
'today():' , datetime.today() -
print
'now():' , datetime.now() -
print
'utcnow():' , datetime.utcnow() -
print
'fromtimestamp(tmstmp):' , datetime.fromtimestamp(time.time()) -
print
'utcfromtimestamp(tmstmp):' , datetime.utcfromtimestamp(time.time()) -
-
#
---- 结果 ---- -
#
datetime.max: 9999-12-31 23:59:59.999999 -
#
datetime.min: 0001-01-01 00:00:00 -
#
datetime.resolution: 0:00:00.000001 -
#
today(): 2010-04-07 09:48:16.234000 -
#
now(): 2010-04-07 09:48:16.234000 -
#
utcnow(): 2010-04-07 01:48:16.234000 # 中国位于+8时间,与本地时间相差8 -
#
fromtimestamp(tmstmp): 2010-04-07 09:48:16.234000 -
#
utcfromtimestamp(tmstmp): 2010-04-07 01:48:16.234000
-
from
datetime import* -
import
time -
-
print
'datetime.max:', datetime.max -
print
'datetime.min:', datetime.min -
print
'datetime.resolution:', datetime.resolution -
print
'today():', datetime.today() -
print
'now():', datetime.now() -
print
'utcnow():', datetime.utcnow() -
print
'fromtimestamp(tmstmp):', datetime.fromtimestamp(time.time()) -
print
'utcfromtimestamp(tmstmp):', datetime.utcfromtimestamp(time.time()) -
-
#
---- 结果 ---- -
#
datetime.max: 9999-12-31 23:59:59.999999 -
#
datetime.min: 0001-01-01 00:00:00 -
#
datetime.resolution: 0:00:00.000001 -
#
today(): 2010-04-07 09:48:16.234000 -
#
now(): 2010-04-07 09:48:16.234000 -
#
utcnow(): 2010-04-07 01:48:16.234000 # 中国位于+8时间,与本地时间相差8 -
#
fromtimestamp(tmstmp): 2010-04-07 09:48:16.234000 -
#
utcfromtimestamp(tmstmp): 2010-04-07 01:48:16.234000
- datetime.year、month、day、hour、minute、second、microsecond、tzinfo:
- datetime.date():获取date对象;
- datetime.time():获取time对象;
- datetime. replace ([ year[ , month[ , day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] ] ] ] ):
- datetime. timetuple ()
- datetime. utctimetuple ()
- datetime. toordinal ()
- datetime. weekday ()
- datetime. isocalendar ()
- datetime. isoformat ([ sep] )
- datetime. ctime ():返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));
- datetime. strftime (format)
格式字符串
格式字符
%a 星期的简写。如 星期三为Web
%A 星期的全写。如 星期三为Wednesday
%b 月份的简写。如4月份为Apr
%B月份的全写。如4月份为April
%c:
%d:
%f:
%H:
%I:
%j:
%m:
%M:
%p:
%S:
%U:
%w:
%W:
%x:
%X:
%y:
%Y:
%z:
%Z:
%%:
例子:
-
dt
= datetime.now() -
print
'(%Y-%m-%d %H:%M:%S ,%f): ' dt.strftime( '%Y-%m-%d%H:%M:%S )%f' -
print
'(%Y-%m-%d %H:%M:%S ,%p): ' dt.strftime( '%y-%m-%d%I:%M:%S )%p' -
print
'%%a: %s ' % '%a' )dt.strftime( -
print
'%%A: %s ' % '%A' )dt.strftime( -
print
'%%b: %s ' % '%b' )dt.strftime( -
print
'%%B: %s ' % '%B' )dt.strftime( -
print
'日期时间%%c: %s ' % '%c' )dt.strftime( -
print
'日期%%x:%s ' % '%x' )dt.strftime( -
print
'时间%%X:%s ' % '%X' )dt.strftime( -
print
'今天是这周的第%s天 ' % '%w' )dt.strftime( -
print
'今天是今年的第%s天 ' % '%j' )dt.strftime( -
print
'今周是今年的第%s周 ' % '%U' )dt.strftime( -
-
#
# ---- 结果 ---- -
#
(%Y-%m-%d %H:%M:%S %f): 2010-04-07 10:52:18 937000 -
#
(%Y-%m-%d %H:%M:%S %p): 10-04-07 10:52:18 AM -
#
%a: Wed -
#
%A: Wednesday -
#
%b: Apr -
#
%B: April -
#
日期时间%c: 04/07/10 10:52:18 -
#
日期%x:04/07/10 -
#
时间%X:10:52:18 -
#
今天是这周的第3天 -
#
今天是今年的第097天 -
#
今周是今年的第14周
-
dt
= datetime.now() -
print
'(%Y-%m-%d %H:%M:%S ,%f): ' dt.strftime( '%Y-%m-%d%H:%M:%S )%f' -
print
'(%Y-%m-%d %H:%M:%S ,%p): ' dt.strftime( '%y-%m-%d%I:%M:%S )%p' -
print
'%%a: %s ' % '%a')dt.strftime( -
print
'%%A: %s ' % '%A')dt.strftime( -
print
'%%b: %s ' % '%b')dt.strftime( -
print
'%%B: %s ' % '%B')dt.strftime( -
print
'日期时间%%c: %s ' % '%c')dt.strftime( -
print
'日期%%x:%s ' % '%x')dt.strftime( -
print
'时间%%X:%s ' % '%X')dt.strftime( -
print
'今天是这周的第%s天 ' % '%w')dt.strftime( -
print
'今天是今年的第%s天 ' % '%j')dt.strftime( -
print
'今周是今年的第%s周 ' % '%U')dt.strftime( -
-
#
# ---- 结果 ---- -
#
(%Y-%m-%d %H:%M:%S %f): 2010-04-07 10:52:18 937000 -
#
(%Y-%m-%d %H:%M:%S %p): 10-04-07 10:52:18 AM -
#
%a: Wed -
#
%A: Wednesday -
#
%b: Apr -
#
%B: April -
#
日期时间%c: 04/07/10 10:52:18 -
#
日期%x:04/07/10 -
#
时间%X:10:52:18 -
#
今天是这周的第3天 -
#
今天是今年的第097天 -
#
今周是今年的第14周