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)
2014-11-17



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



詳細文檔請查看:

https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior



二,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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章