天天看点

python datetime和calendar模块常用功能

一,datetime模块

datetime模块定义了下面这几个类:

datetime.date:表示日期的类。常用的属性有year, month, day;

datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;

datetime.datetime:表示日期和时间。

datetime.timedelta:表示时间间隔,即两个时间点之间的长度。

datetime.tzinfo:与时区有关的相关信息。

1,datetime.datetime对象

显示现在的时间及自定义格式

In [10]: str_time = datetime.datetime.now()

In [10]: str_time = datetime.datetime.today()

In [44]: print str_time

2016-01-29 16:42:05.545688

显示格式自定义:

In [45]: print str_time.strftime("%Y-%m-%d %H:%M")

2016-01-29 16:42

把当前utc时间转换为时间戳:

In [47]: time.mktime(str_time.timetuple())

Out[47]: 1454056925.0

将时间格式的字符串转换为datetime对象

In [49]: a="2014 11-17 20:02"

In [50]: print datetime.datetime.strptime(a, "%Y %m-%d %H:%M")

2014-11-17 20:02:00

显示当前的utc时间

In [55]: print datetime.datetime.utcnow()

2014-11-17 12:26:59.612081

把(unix)时间戳转化为datetime对象:

In [64]: a=time.time()

In [65]: print a

1416227538.62

In [66]: print datetime.datetime.fromtimestamp(a)

2014-11-17 20:32:18.619621

In [67]: print datetime.datetime.utcfromtimestamp(a)

2014-11-17 12:32:18.619621

显示对应时间是年中的第几周:

In [10]: print datetime.datetime(2014,12,24).isocalendar()

(2014, 52, 3)

注:

2014:元组的第一个值是日期对应的年份

52:元组的第二个值是日期对应2014年中的第几周

3:元组的第三个值是日期对应的周号

2,datetime.date对象

显示当前日期:

In [86]: str_date = datetime.date.today()

In [87]: print str_date

2014-11-17

显示date对象的年月日:

In [93]: print str_date.year,str_date.month,str_date.day

2014 11 17

把(unix)时间戳转化为date对象:

In [96]: a=time.time()

In [97]: print a

1416228263.01

In [98]: print datetime.date.fromtimestamp(a)

3,datetime.time对象

time类表示时间,由时、分、秒以及微秒组成。

In [109]: tm = datetime.time(23 , 46 , 10 )

In [110]: print tm.hour,tm.minute,tm.second

23 46 10

In [113]: tm2 = tm.replace(minute=01)

In [114]: print tm2.hour,tm2.minute,tm2.second

23 1 10

4,datetime.timedelta对象

显示昨天的日期(也可以是时间的上几min或者几秒或者几小时)

In [131]: a = datetime.datetime.now()

In [132]: print a

2014-11-17 20:57:17.170393

In [133]: print a + datetime.timedelta(days=-1)

2014-11-16 20:57:17.170393

In [137]: print a + datetime.timedelta(days=1)

2014-11-18 20:57:17.170393

In [134]: print a + datetime.timedelta(hours=-1)

2014-11-17 19:57:17.170393

In [135]: print a + datetime.timedelta(minutes=-1)

2014-11-17 20:56:17.170393

In [136]: print a + datetime.timedelta(seconds=-1)

2014-11-17 20:57:16.170393

5,两个datetime.datetime对象时间差计算

计算秒差值或者天的差值

In [180]: a = datetime.datetime.now()

In [181]: b = a + datetime.timedelta(days=-2)

In [182]: c = a - b

In [183]: print c.total_seconds()

172800.0

In [184]: print c.days

2

详细文档请查看:

<a href="https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior" target="_blank">https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior</a>

二,calendar模块

显示某天是周号

In [17]: calendar.weekday(2015,1,8)

Out[17]: 3

显示某月的所有周:

In [18]: cal = calendar.Calendar()

In [19]: cal.monthdatescalendar(2015, 1)

显示某年的所有周:

In [24]: cal = calendar.Calendar()

In [25]: cal.yeardatescalendar(2015)

显示某月份有几天

In [190]: print calendar.monthrange(2014, 11)

(5, 30)

(5, 30)解释:5表示2014年11月份的第一天是周六;30表示2014年11月份总共有30天

判断是否为润年

In [194]: print calendar.isleap(2012)

True

In [195]: print calendar.isleap(2014)

False

判断两个年份之间,润年的个数

In [196]: print calendar.leapdays(2000, 2014)

4

显示某月份的日历:

In [206]: cal = calendar.month(2011, 11)

In [207]: print cal

November 2011

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

https://docs.python.org/2/library/calendar.html

本文转自 leejia1989 51CTO博客,原文链接:http://blog.51cto.com/leejia/1577662,如需转载请自行联系原作者