python-时间


时间 time

在Python中,与时间处理相关的模块有:time、datetime以及calendar。

学会计算时间,对程序的调优非常重要,可以在程序中狂打时间戳,来具体判断程序中哪一块耗时最多,从而找到程序调优的重心处。

在Python中,通常有这几种方式表示时间:时间戳、格式化的时间字符串、元组(struct_time 共九种元素)。由于Python的time模块主要是调用C库实现的,所以在不同的平台可能会有所不同。

时间戳(timestamp)的方式:时间戳表示是从1970年1月1号 00:00:00开始到现在按秒计算的偏移量。Tick单位(时间间隔以秒为单位的浮点小数)最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年某日。查看一下type(time.time())的返回值类型,可以看出是float类型。返回时间戳的函数主要有time()、clock()等。

UTC(世界协调时),就是格林威治天文时间,也是世界标准时间。在中国为UTC+8。DST夏令时。

元组方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。

时区(Time Zone)是地球上的区域使用同一个时间定义。1884年在华盛顿召开国际经度会议时,为了克服时间上的混乱,规定将全球划分为24个时区。

time.localtime()时间元祖:

>>> import time

>>> time.localtime()

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=30, tm_hour=22, tm_min=41, tm_sec=56, tm_wday=1, tm_yday=30, tm_isdst=0)

 

 

 

 

既然是一个元组,那么就遵循元组的所有特性,比如索引(都从0开始),切片等。下面是元组中各元素的解释:

 tm_year:年

tm_mon:月(1-12)

tm_mday:日(1-31)

tm_hour:时(0-23)

tm_min:分(0-59)

tm_sec:秒(0-59)

tm_wday:星期几(0-6,6表示周日)

tm_yday:一年中的第几天(1-366)

tm_isdst:是否是夏令时(默认为-1)

>>>t=time.localtime()

>>> t

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=30, tm_hour=22, tm_min=47, tm_sec=45, tm_wday=1, tm_yday=30, tm_isdst=0)

>>> dir(t)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'n_fields', 'n_sequence_fields', 'n_unnamed_fields', 'tm_hour', 'tm_isdst', 'tm_mday', 'tm_min', 'tm_mon', 'tm_sec', 'tm_wday', 'tm_yday', 'tm_year']

>>> 

由上可见,localtime()是有一些内置属性的,类似于string的letters一样,可以用t.tm_mday来方位tm_mday这个属性的值

练习定义两个函数,返回年月日,时分秒

#encoding=utf-8

 

def get_day():

    import time

    time_t=time.localtime()

    return u"现在是%s年%s月%s日" %(time_t.tm_year,time_t.tm_mon,time_t.tm_mday)

print get_day()

 

def get_sec():

    import time

    time_t=time.localtime()

    return u"现在是%s时%s分%s秒" %(time_t.tm_hour,time_t.tm_min,time_t.tm_sec)

print get_sec()

 

c:\Python27\Scripts>python task_test.py

现在是2018130

现在是225746

 

 

Time.localtime()可以用下标取值

>>> import time

>>> time.localtime()

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=23, tm_min=9, tm_sec=34, tm_wday=2, tm_yday=31, tm_isdst=0)

>>> time.localtime()[0]

2018

>>> time.localtime()[1]

1

>>> time.localtime()[2]

31

>>> 

 

格林威治时间,英国

>>> time.gmtime()

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=15, tm_min=10, tm_sec=0, tm_wday=2, tm_yday=31, tm_isdst=0)

时间戳time.time()

>>> time.time()

1517411441.422

时间戳取整,用int或者用%.f小数点位数

>>> time.time()

1517411441.422

>>> type(time.time())

<type 'float'>

>>> int(time.time())

1517411467

>>> "%.f"%time.time()

'1517411621'

>>> "%.1f"%time.time()

'1517411632.6'

>>> "%.2f"%time.time()

'1517411635.87'

用时间戳通过localtime()生成时间

>>> time.localtime(1517411635.87)

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=23, tm_min=13, tm_sec=55, tm_wday=2, tm_yday=31, tm_isdst=0)

 

练习:实现函数转换时间戳到时间

#encoding=utf-8

 

#encoding=utf-8

import time

 

def get_year_mon_day(timestamp=None):

    if timestamp ==None:

        time_array=time.localtime()

    else:

        try:

            time_array=time.localtime(timestamp)

        except:

            print "time convert error occur!"

            return None

    return u"%s年%s月%s日" % (time_array.tm_year,time_array.tm_mon,time_array.tm_mday)

   

print get_year_mon_day(1517411635.87)

 

print get_year_mon_day()

 

 

def get_hour_min_sec(timestamp=None):

    if timestamp ==None:

        time_array=time.localtime()

    else:

        try:

            time_array=time.localtime(timestamp)

        except:

            print "time convert error occur!"

            return None

 

    return u"%s时%s分%s秒" %(time_array.tm_hour,time_array.tm_min,time_array.tm_sec)

 

print get_hour_min_sec(1517411635.8)

print get_hour_min_sec()

 

c:\Python27\Scripts>python task_test.py

2018131

2018131

231355

232953

 

Time.mktime()将时间元祖转换到时间戳

>>> time_array=time.localtime()

>>> time_array

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=23, tm_min=31, tm_sec=3, tm_wday=2, tm_yday=31, tm_isdst=0)

>>> time.mktime(time_array)

1517412663.0

>>> time.mktime(time_array)

1517412663.0

 

Time.gmtime()将时间戳转为UTC时间

>>> time.time()

1517412894.745

>>> time.gmtime(1517412894.745)

time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=15, tm_min=34, tm_sec=54, tm_wday=2, tm_yday=31, tm_isdst=0)

 

Time.sleep()睡眠

Time. clock()CPU的累计时间,不同时间的time.clock()相减计算时间差

>>> import time

>>> time.clock()

19.208307247590035

>>> time.clock()

20.248680169227494

>>> time.clock()

20.712062032827205

 

格式化时间字符串time.strftime,time的常用函数—strftime

返回字符串表示的当地时间

把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串,格式由参数format决定。如果未指定,将传入time.localtime()。如果元组中任何一个元素越界,就会抛出ValueError的异常。函数返回的是一个可读表示的本地时间的字符串。

参数:

format:格式化字符串

t :可选的参数是一个struct_time对象

 

 

>>> strTime=time.strftime("%Y-%m-%d-%H:%M:%S",time.localtime())

>>> strTime

'2018-02-01-20:15:50'

>>> print time.strftime("%Y-%m-%d-%a:%M:%b:%c",time.localtime())

2018-02-01-Thu:17:Feb:02/01/18 20:17:17

>>> print time.strftime("%Y-%m-%d-%a:%M:%b:%c",time.localtime())

2018-02-01-Thu:17:Feb:02/01/18 20:17:17

>>> print time.strftime("%S",time.localtime())

53

>>> print time.strftime("%X",time.localtime())

20:17:57

>>> print time.strftime("%X-%xx",time.localtime())

20:18:02-02/01/18x

>>> print time.strftime("%X-%x",time.localtime())

20:18:08-02/01/18

>>> print time.strftime("%X-%x-%y-%Y-%z-%Z",time.localtime())

20:18:36-02/01/18-18-2018-中国标准时间-中国标准时间

>>> print time.strftime("%X-%x-%y-%Y-%z-%Z-%z-%Z",time.localtime())

20:19:01-02/01/18-18-2018-中国标准时间-中国标准时间-中国标准时间-中国标准时间

>>> 

 

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:20:24

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:20:39

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:20:56

>>> print time.strftime("%Y/%m/%d %H:%M:%S",time.localtime())

2018/02/01 20:21:08

 

练习,在文件模式下中文显示时间

 

#encoding=utf-8

 

import time

 

 

print time.strftime("%Y%m%d-%H:%M:%S",time.localtime()).decode('utf-8')

print type(time.strftime("%Y%m%d-%H:%M:%S",time.localtime()).decode('utf-8'))

c:\Python27\Scripts>python task_test.py

20180201-20:32:07

<type 'unicode'>

 

time.strptime把具体的时间字符串转换为时间元祖

将格式字符串转化成struct_time.

该函数是time.strftime()函数的逆操作。time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组。所以函数返回的是struct_time对象。

参数:

string :时间字符串

format:格式化字符串

>>> stime="2015-08-24 13:01:30"

>>> print time.strptime(stime,"%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2015, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=1, tm_sec=30, tm_wday=0, tm_yday=236, tm_isdst=-1)

>>> format_time=time.strptime(stime,"%Y-%m-%d %H:%M:%S")

>>> format_time

time.struct_time(tm_year=2015, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=1, tm_sec=30, tm_wday=0, tm_yday=236, tm_isdst=-1)

>>> for i in format_time:

...     print i

...

2015

8

24

13

1

30

0

236

-1

 

注意在使用strptime()函数将一个指定格式的时间字符串转化成元组时,参数format的格式必须和string的格式保持一致,如果string中日期间使用“-”分隔,format中也必须使用“-”分隔,时间中使用冒号“:”分隔,后面也必须使用冒号分隔,否则会报格式不匹配的错误

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=20, tm_min=42, tm_sec=14, tm_wday=3, tm_yday=32, tm_isdst=-1)

 

下面因为后面的格式匹配,报错

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=20, tm_min=42, tm_sec=14, tm_wday=3, tm_yday=32, tm_isdst=-1)

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%X")

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "C:\Python27\lib\_strptime.py", line 478, in _strptime_time

    return _strptime(data_string, format)[0]

  File "C:\Python27\lib\_strptime.py", line 315, in _strptime

    format_regex = _TimeRE_cache.compile(format)

  File "C:\Python27\lib\_strptime.py", line 269, in compile

    return re_compile(self.pattern(format), IGNORECASE)

  File "C:\Python27\lib\re.py", line 194, in compile

    return _compile(pattern, flags)

  File "C:\Python27\lib\re.py", line 251, in _compile

    raise error, v # invalid expression

sre_constants.error: redefinition of group name 'H' as group 6; was group 4

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%X")

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "C:\Python27\lib\_strptime.py", line 478, in _strptime_time

    return _strptime(data_string, format)[0]

  File "C:\Python27\lib\_strptime.py", line 315, in _strptime

    format_regex = _TimeRE_cache.compile(format)

  File "C:\Python27\lib\_strptime.py", line 269, in compile

    return re_compile(self.pattern(format), IGNORECASE)

  File "C:\Python27\lib\re.py", line 194, in compile

    return _compile(pattern, flags)

  File "C:\Python27\lib\re.py", line 251, in _compile

    raise error, v # invalid expression

sre_constants.error: redefinition of group name 'H' as group 6; was group 4

>>> time.strptime("2018-02-01 20:42:14","%Y-%m-%d %H:%M:%S")

 

>>> time.strptime(stime,"%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=20, tm_min=42, tm_sec=14, tm_wday=3, tm_yday=32, tm_isdst=-1)

 

Python datetime模块详解

datetime模块是Python中另一个用于操作时间的内置模块,是基于time包的一个高级包。相比于time模块,datetime模块的接口更直观、更容易调用。datetime可以理解为datetime两个部分组成,date是指年月日构成的日期(相当于日历),time是指时分秒微妙构成的一天24小时中的具体时间(相当于手表)。由此可以将这两个分开管理(datetime.date类,datetime.time类),也可以将两者合并在一起(datetime.datetime类)。下面就分别介绍一下datetime模块中的这几个类。 引入datetime模块: import datetime(或 from datetime import * datetime模块定义了两个常量: datetime.MINYEAR :最小年份,MINYEAR = 1 datetime.MAXYEAR:最大年份。MAXYEAR = 9999 datetime模块定义了下面几个类: datetime.date:日期类。常用的属性有yearmonthday datetime.time:时间类。常用的属性有hourminutesecondmicrosecond datetime.timedalta:表示时间间隔,即两个时间点之间的长度。 datetime.tzinfo:与时区有关的信息。 以上这些类都是datetime模块中的类,但是需要注意的是这些类中的对象都是不可被改变的。下面来具体讲解一下这些类的使用方法。

Datetime.date.today()

>>> print datetime.date.today()

2018-02-01

 

通过时间戳转换为当前的年月日

>>> import time

>>> import datetime

#获取当前时间的时间戳

>>> now=time.time()

>>> now

1517489601.47

#将时间戳转换为date类型的时间

>>> s=datetime.date.fromtimestamp(now)

>>> s

datetime.date(2018, 2, 1)

>>> print s

 

2018-02-01

 

 

 

date的常用函数—date.weekday函数

 

返回weekday中的星期几,星期一,返回0;星期二,返回1;以此类推。

 

该函数需要一个datetime.date类型的参数。

 

>>> import datetime

 

>>> now=datetime.date.today()

 

>>> print now

 

2018-02-02

 

>>> print datetime.date.weekday(now)

 

4

 

 

 

now.strftime()转化成指定格式的年月日

 

>>> import datetime

 

>>> now=datetime.datetime.now()

 

>>> print now

 

2018-02-02 21:40:20.250000

 

>>> otherStyleTime=now.strftime("%Y-%m-%d %H:%M:%S")

 

>>> print otherStyleTime

 

2018-02-02 21:40:20

 

 

 

timedelta,基于年月日算时间差时间加上时间间隔

 

>>> from datetime import *

 

>>> today=date.today()#获取今天的日期

 

>>> print today

 

2018-02-02

 

#在今天的日期上再加上10

 

>>> print today+timedelta(days=10)

 

2018-02-12

 

 

 

#encoding=utf-8

 

 

 

from datetime import *

 

#获取今天的日期

 

today=date.today()

 

print today

 

#在今天的日期上减去10天

 

 

 

print today-timedelta(days=10)

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02

 

2018-01-23

 

 

 

两个日期相减算时间差

 

#encoding=utf-8

 

 

 

from datetime import *

 

#获取今天的日期

 

today=date.today()

 

print today

 

 

 

#替换形成一个新的日期

 

future=today.replace(day=15)

 

print future

 

 

 

#算一个两日相隔的间隔

 

delta=future-today

 

print delta

 

 

 

#在原日期上添加一个日期间隔

 

print future+delta

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02

 

2018-02-15

 

13 days, 0:00:00

 

2018-02-28

 

 

 

只取相差天数

 

 

 

#encoding=utf-8

 

 

 

from datetime import *

 

#获取今天的日期

 

today=date.today()

 

print today

 

 

 

#替换形成一个新的日期

 

future=today.replace(day=15)

 

print future

 

 

 

#算一个两日相隔的间隔

 

delta=future-today

 

print str(delta)

 

print str(delta).split()

 

print str(delta).split()[0]

 

 

 

#在原日期上添加一个日期间隔

 

print future+delta

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02

 

2018-02-15

 

13 days, 0:00:00

 

['13', 'days,', '0:00:00']

 

13

 

2018-02-28

 

 

 

比较时间大小

 

#encoding=utf-8

 

 

 

from datetime import *

 

#今天的日期

 

now=date.today()

 

print now

 

 

 

#未来的日期

 

tomorrow=now.replace(day=13)

 

print tomorrow

 

#比较两日期大小

 

print tomorrow > now

 

 

 

time类

 

#coding=utf-8

 

 

 

from datetime import *

 

#tm=time(23,46,10

 

tm=datetime.now()

 

print tm

 

print tm.hour

 

print tm.minute

 

print tm.second

 

print tm.microsecond

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 22:25:54.539000

 

22

 

25

 

54

 

539000

 

 

 

>>> from datetime import *

 

>>> tm=datetime.now()

 

>>> print tm

 

2018-02-02 22:27:45.876000

 

>>> print tm.year

 

2018

 

>>> print tm.month

 

2

 

>>> print tm.day

 

2

 

>>> print tm.hour

 

22

 

>>> print tm.second

 

45

 

>>> print tm.minute

 

27

 

>>> print tm.microsecond

 

876000

 

 

 

datetime.now()

 

>>> import time

 

>>> from datetime import *

 

>>> datetime.now

 

<built-in method now of type object at 0x000000006B870340>

 

>>> datetime.now()

 

datetime.datetime(2018, 2, 2, 22, 29, 12, 628000)

 

>>> 

 

replace()时间替换

 

>>> from datetime import *

 

>>> tm=datetime.now()

 

>>> print tm

 

2018-02-02 22:29:55.649000

 

>>> tm1=tm.replace(hour=12,minute=10,second=10)

 

>>> print tm1

 

2018-02-02 12:10:10.649000

 

>>> 

 

tm=time(23, 46, 10)=> 23:46:10用time()可以指定时分秒

 

tm.strftime("%H-%M-%S")strftime()对具体时间进行格式化

 

 

 

>>> from datetime import *

 

>>> tm=time(23,46,10)

 

>>> print tm

 

23:46:10

 

>>> tm1=tm.strftime("%H-%M-%S")

 

>>> print tm1

 

23-46-10

 

>>> 

 

 

 

Datetime.strptime()将格式时间字符串转换为datetime对象

 

>>> stime=datetime.datetime.strptime("2015-08-27 17:23:43","%Y-%m-%d %H:%M:%S")

 

>>> stime

 

datetime.datetime(2015, 8, 27, 17, 23, 43)

 

>>> type(stime)

 

<type 'datetime.datetime'>

 

>>> stime.day

 

27

 

>>> stime.year

 

2015

 

>>> stime.second

 

43

 

>>> import datetime

 

>>> print datetime.datetime.strptime("2015-08-27 17:23:43","%Y-%m-%d %H:%M:%S")

 

2015-08-27 17:23:43

 

 

 

times.replace(year = 2017, hour = 23)

 

 

 

#coding=utf-8

 

import datetime

 

times=datetime.datetime.today()

 

print times

 

#替换原有时间,获得新的日期时间

 

tmp=times.replace(year=2017,hour=23)

 

print tmp

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 22:45:41.797000

 

2017-02-02 23:45:41.797000

 

 

 

将datetime对象转换成时间戳

 

 

 

#coding=utf-8

 

 

 

import datetime

 

import time

 

 

 

#获取当前时间的datetime对象

 

t=datetime.datetime.now()

 

print t

 

#根据当前时间的datetime对象获取当前时间的时间元祖

 

struct_time=t.timetuple()

 

print struct_time

 

#根据当前时间的时间元祖获得当前时间的时间戳

 

timeStamp=time.mktime(struct_time)

 

#对时间戳取整

 

timeStamp=int(timeStamp)

 

print timestamp

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 22:53:53.935000

 

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=2, tm_hour=22, tm_min=53, tm_sec=53, tm_wday=4, tm_yday=33, tm_isdst=-1)

 

1517583233

 

 

 

 

 

Datetime对象通过timetuple()函数转换到时间元祖

 

>>> datetime.datetime.today()

 

datetime.datetime(2018, 2, 2, 22, 50, 13, 600000)

 

>>> datetime.datetime.today().timetuple()

 

time.struct_time(tm_year=2018, tm_mon=2, tm_mday=2, tm_hour=22, tm_min=50, tm_sec=26, tm_wday=4, tm_yday=33, tm_isdst=-1)

 

>>> print datetime.datetime.today()

 

2018-02-02 22:50:36.262000

 

>>> 

 

求天数差

 

>>> import datetime

 

>>> d1=datetime.datetime(2015,7,5)

 

>>> d2=datetime.datetime(2015,8,26)

 

>>> print d1

 

2015-07-05 00:00:00

 

>>> print d2

 

2015-08-26 00:00:00

 

>>> print d2-d1

 

52 days, 0:00:00

 

>>> print (d2-d1).days

 

52

 

>>> print (d2-d1).seconds

 

0

 

datetime.datetime(2015,7,5)= 2015-07-05 00:00:00

 

 

 

timedelta类

 

>>> import datetime

 

>>> print datetime.timedelta(days=1)

 

1 day, 0:00:00

 

 

 

#coding=utf-8

 

 

 

import datetime

 

import time

 

 

 

#获取当前的时间

 

now = datetime.datetime.now()

 

print now

 

 

 

#设定一个时间间隔,间隔为1天

 

delta=datetime.timedelta(days=1)

 

newTime=now+delta

 

print newTime

 

 

 

#得到明天的时间

 

print str(newTime)[:-7]

 

 

 

#或者使用格式化函数

 

print newTime.strftime("%Y-%m-%d %H:%M:%S")

 

 

 

#原时间减一天

 

delta=datetime.timedelta(days=-1)

 

newTime=now+delta

 

print newTime

 

 

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 23:09:27.367000

 

2018-02-03 23:09:27.367000

 

2018-02-03 23:09:27

 

2018-02-03 23:09:27

 

2018-02-01 23:09:27.367000

 

 

 

 

 

算100天前的日期

 

#coding=utf-8

 

from datetime import datetime

 

from datetime import timedelta

 

 

 

now = datetime.now()

 

#设置100天前的时间间隔

 

delta = timedelta(days = -100)

 

#得到100天前的时间

 

oldTime = now + delta

 

print oldTime.strftime("%Y-%m-%d")

 

c:\Python27\Scripts>python task_test.py

 

2017-10-25

 

 

 

按小时算小时的时间差

 

#coding=utf-8

 

import datetime

 

 

 

threeHoursAgo = datetime.datetime.now() - datetime.timedelta(hours = 3)

 

print datetime.datetime.now()

 

print str(threeHoursAgo)[:-7]

 

c:\Python27\Scripts>python task_test.py

 

2018-02-02 23:14:45.110000

 

2018-02-02 20:14:45

 

 

 

 

 

计算给定时间间隔的总秒数

 

#coding=utf-8

 

import datetime

 

#计算给定时间间隔的总秒数

 

seconds = datetime.timedelta(hours=1, seconds=30).total_seconds()

 

print seconds

 

c:\Python27\Scripts>python task_test.py

 

3630.0

 

 

 

 

 

calendar日历

 

calendar.isleap(2015) 判断是否是闰年

 

import calendar

 

#判断2015年是否是闰年

 

if calendar.isleap(2015) is True :

 

  print u"2015年是闰年"

 

else :

 

  print u"2015年不是闰年"

 

 

 

c:\Python27\Scripts>python task_test.py

 

2015年不是闰年

 

打印日历

 

 

 

#coding=utf-8

 

 

 

import calendar

 

print calendar.month(2015, 3, 1, 1)

 

c:\Python27\Scripts>python task_test.py

 

     March 2015

 

Mo Tu We Th Fr Sa Su

 

                   1

 

 2  3  4  5  6  7  8

 

 9 10 11 12 13 14 15

 

16 17 18 19 20 21 22

 

23 24 25 26 27 28 29

 

30 31

 

 

 

年历

 

#coding=utf-8

 

 

 

import calendar

 

print calendar.calendar(2015, 3, 1, 1)

 

 

 

 

 

c:\Python27\Scripts>python task_test.py

 

                                         2015

 

 

 

          January                      February                      March

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

              1   2   3   4                            1                            1

 

  5   6   7   8   9  10  11    2   3   4   5   6   7   8    2   3   4   5   6   7   8

 

 12  13  14  15  16  17  18    9  10  11  12  13  14  15    9  10  11  12  13  14  15

 

 19  20  21  22  23  24  25   16  17  18  19  20  21  22   16  17  18  19  20  21  22

 

 26  27  28  29  30  31       23  24  25  26  27  28       23  24  25  26  27  28  29

 

                                                           30  31

 

 

 

           April                         May                          June

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

          1   2   3   4   5                    1   2   3    1   2   3   4   5   6   7

 

  6   7   8   9  10  11  12    4   5   6   7   8   9  10    8   9  10  11  12  13  14

 

 13  14  15  16  17  18  19   11  12  13  14  15  16  17   15  16  17  18  19  20  21

 

 20  21  22  23  24  25  26   18  19  20  21  22  23  24   22  23  24  25  26  27  28

 

 27  28  29  30               25  26  27  28  29  30  31   29  30

 

 

 

            July                        August                     September

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

          1   2   3   4   5                        1   2        1   2   3   4   5   6

 

  6   7   8   9  10  11  12    3   4   5   6   7   8   9    7   8   9  10  11  12  13

 

 13  14  15  16  17  18  19   10  11  12  13  14  15  16   14  15  16  17  18  19  20

 

 20  21  22  23  24  25  26   17  18  19  20  21  22  23   21  22  23  24  25  26  27

 

 27  28  29  30  31           24  25  26  27  28  29  30   28  29  30

 

                              31

 

 

 

          October                      November                     December

 

Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun  Mon Tue Wed Thu Fri Sat Sun

 

              1   2   3   4                            1        1   2   3   4   5   6

 

  5   6   7   8   9  10  11    2   3   4   5   6   7   8    7   8   9  10  11  12  13

 

 12  13  14  15  16  17  18    9  10  11  12  13  14  15   14  15  16  17  18  19  20

 

 19  20  21  22  23  24  25   16  17  18  19  20  21  22   21  22  23  24  25  26  27

 

 26  27  28  29  30  31       23  24  25  26  27  28  29   28  29  30  31

 

                              30

 

Html calendar

 

生成html代码

 

import calendar
myCal = calendar.HTMLCalendar(calendar.SUNDAY)
print myCal.formatmonth(2016, 7)
with open('calendar.html','w') as fp:
    fp.write(myCal.formatmonth(2016, 7))

 

然后放到html文件里

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM