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 : str_time = datetime.datetime.now()
或
In : str_time = datetime.datetime.today()
In : print str_time
2016-01-29 16:42:05.545688
显示格式自定义:
In : print str_time.strftime("%Y-%m-%d %H:%M")
2016-01-29 16:42
把当前utc时间转换为时间戳:
In : time.mktime(str_time.timetuple())
Out: 1454056925.0
将时间格式的字符串转换为datetime对象
In : a="2014 11-17 20:02"
In : print datetime.datetime.strptime(a, "%Y %m-%d %H:%M")
2014-11-17 20:02:00
显示当前的utc时间
In : print datetime.datetime.utcnow()
2014-11-17 12:26:59.612081
把(unix)时间戳转化为datetime对象:
In : a=time.time()
In : print a
1416227538.62
In : print datetime.datetime.fromtimestamp(a)
2014-11-17 20:32:18.619621
In : print datetime.datetime.utcfromtimestamp(a)
2014-11-17 12:32:18.619621
显示对应时间是年中的第几周:
In : print datetime.datetime(2014,12,24).isocalendar()
(2014, 52, 3)
注:
2014:元组的第一个值是日期对应的年份
52:元组的第二个值是日期对应2014年中的第几周
3:元组的第三个值是日期对应的周号
2,datetime.date对象
显示当前日期:
In : str_date = datetime.date.today()
In : print str_date
2014-11-17
显示date对象的年月日:
In : print str_date.year,str_date.month,str_date.day
2014 11 17
把(unix)时间戳转化为date对象:
In : a=time.time()
In : print a
1416228263.01
In : print datetime.date.fromtimestamp(a)
2014-11-17
3,datetime.time对象
time类表示时间,由时、分、秒以及微秒组成。
In : tm = datetime.time(23 , 46 , 10 )
In : print tm.hour,tm.minute,tm.second
23 46 10
In : tm2 = tm.replace(minute=01)
In : print tm2.hour,tm2.minute,tm2.second
23 1 10
4,datetime.timedelta对象
显示昨天的日期(也可以是时间的上几min或者几秒或者几小时)
In : a = datetime.datetime.now()
In : print a
2014-11-17 20:57:17.170393
In : print a + datetime.timedelta(days=-1)
2014-11-16 20:57:17.170393
In : print a + datetime.timedelta(days=1)
2014-11-18 20:57:17.170393
In : print a + datetime.timedelta(hours=-1)
2014-11-17 19:57:17.170393
In : print a + datetime.timedelta(minutes=-1)
2014-11-17 20:56:17.170393
In : print a + datetime.timedelta(seconds=-1)
2014-11-17 20:57:16.170393
5,两个datetime.datetime对象时间差计算
计算秒差值或者天的差值
In : a = datetime.datetime.now()
In : b = a + datetime.timedelta(days=-2)
In : c = a - b
In : print c.total_seconds()
172800.0
In : print c.days
2
详细文档请查看:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
二,calendar模块
显示某天是周号
In : calendar.weekday(2015,1,8)
Out: 3
显示某月的所有周:
In : cal = calendar.Calendar()
In : cal.monthdatescalendar(2015, 1)
显示某年的所有周:
In : cal = calendar.Calendar()
In : cal.yeardatescalendar(2015)
显示某月份有几天
In : print calendar.monthrange(2014, 11)
(5, 30)
(5, 30)解释:5表示2014年11月份的第一天是周六;30表示2014年11月份总共有30天
判断是否为润年
In : print calendar.isleap(2012)
True
In : print calendar.isleap(2014)
False
判断两个年份之间,润年的个数
In : print calendar.leapdays(2000, 2014)
4
显示某月份的日历:
In : cal = calendar.month(2011, 11)
In : 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
页:
[1]