1. 程式人生 > >datetime模組+calendar模組

datetime模組+calendar模組

datetime:

"""
模組中的類:
datatime    同時有時間和日期
timedelta   主要用於計算時間的跨度
tzinfo      時區相關
time        只關注時間
date        只關注日期

"""
import datetime

#獲取當前時間
d1 = datetime.datetime.now()
print(d1)
print(type(d1))

#獲取指定的時間
d2 = datetime.datetime(1999,10,1,10,28,25,564825)
print(d2)

#將時間轉為字串
d3 = d1.strftime("
%y-%m-%d %X") print(d3) print(type(d3)) #將格式化字串轉為datetime物件 #轉換的格式要與字串一致 d4 = datetime.datetime.strptime(d3,"%y-%m-%d %X") print(d4) print(type(d4)) d5 =datetime.datetime(1999,10,1,10,28,25,123456) d6 = datetime.datetime.now() d7 = d6-d5 print(d7) print(type(d7)) #間隔的天數 print(d7.days) #間隔天數除外的秒數 print
(d7.seconds)

calendar:

 

import calendar

"""
日曆模組
"""

#使用

#返回指定某年某月的日曆
print(calendar.month(2018,11))

#返回指定年的日曆
# print(calendar.calendar(2018))

#閏年返回True, 否則返回False
# print(calendar.isleap(2018))

#返回某個月的weekday的第一天和這個月所有的天數
# print(calendar.monthrange(2018,11))

#返回某個月以每一週為元素的列表
print
(calendar.monthcalendar(2018,7))