time模組
time模組是包含各方面對時間操作的函數. 儘管這些常常有效但不是所有方法在任意平台中有效.time 用struct_time表示時間
import time # time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24, tm_hour=14, tm_min=17, tm_sec=26, tm_wday=4, tm_yday=114, tm_isdst=0) # 2015 print time.localtime() print time.localtime().tm_year
函數
#time.time(): 傳回一個時間戳
time.asctime([t]): 轉換gmtime()和localtime()傳回的元組或struct_time為string.
time.clock(): 在第一次呼叫的時候, 傳回程式運行的時間. 第二次之後傳回與先前的間隔.
import time # Fri Apr 24 06:39:34 2015 print time.asctime(time.gmtime()) # 0.0 # None # 1.01136392961 因计算机而异 print time.clock() print time.sleep(1) print time.clock() # Fri Apr 24 14:42:07 2015 print time.ctime() # 2015-04-24 print time.strftime('%Y-%m-%d', time.localtime()) # 1429857836.0 print time.mktime(time.localtime())
time模組中常用的格式化字串
#datetime模組
datetime模組提供對於日期和時間進行簡單或複雜的操作. datetime 模組提供了一下的可用類型(Available Types).
from datetime import timedelta, datetime a = datetime.now() b = timedelta(days=7) # 7 days, 0:00:00 # 2015-04-14 16:02:39.189000 print b print a - b下面說具體說一下類別和類別的方法
date類別
一個date物件代表理想化的日期.class datetime.date(year, month, day) # All arguments are required. Arguments may be ints or longs. # 所有参数都是必须的. 参数可能是 int 或 long. MINYEAR <= year <= MAXYEAR 1<= month <= 12 1<= day <= number of days in the given month and year.(随着月份和年份)如果參數脫離給的範圍會拋出, valueError.
1.類別方法>`date.today()`:傳回目前的本機日期, 這等價於`date.fromtimestamp(time.time())`.
Return the current local date. This is equvalent to `date.fromtimestamp(time.time())`.
from datetime import date # print 2015-04-21 print date.today()2.date.fromtimestamp(timestamp):根據提供的時間戳返回local date. 時間戳常用於對時間類型的存儲.
import time from datetime import date # 1429587111.21 # 2015-04-21 print time.time() print date.fromtimestamp(time.time())3.類別方法date.fromordinal(ordinal):根據提供的Gregorian日曆回傳date.(不做描述)類別屬性
d = date(2014, 4, 21) # 2014 4 21 print d.year, d.month, d.day實例方法
#
d = date(2015, 4, 21) # 2015-04-21 # 2015-04-21 # 2015-04-22 print d print d.replace() print d.replace(day=22) # time.struct_time(tm_year=2015, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=111, tm_isdst=-1) print d.timetuple() # print 1 # print 2 print d.weekday() print d.isoweekday() # print 2015-04-21 print d.isoformat() # print 21/04/2015 print d.strftime('%d/%m/%y')
datetime 類別
datetime 物件是一個單一的物件, 包含所有date和time物件的資訊.class datetime.datetime(year, month, day[, hour [, minute [, second [, microsecond [, tzinfo]]]]]) # The year, month and day arguments are required. MINYEAR <= year <= MAXYEAR 1 <= month <= 12 1 <= day <= n 0 <= hour < 24 0 <= minute < 60 0 <= second < 60 0 <= microsecond < 10**6類別方法
datetime.now([tz]): 返回当前本地日期和时间, 如果可选参数tz为None或没有详细说明,这个方法会像today().
datetime.utcnow(): 返回当前的UTC日期和时间, 如果tzinfo None ,那么与now()类似.
datetime.fromtimestamp(timestamp[, tz]): 根据时间戳返回本地的日期和时间.tz指定时区.
datetime.utcfromtimestamp(timestamp): 根据时间戳返回 UTC datetime.
datetime.fromordinal(ordinal): 根据Gregorian ordinal 返回datetime.
datetime.combine(date, time): 根据date和time返回一个新的datetime.
datetime.strptime(date_string, format): 根据date_string和format返回一个datetime.
from datetime import datetime # 2015-04-21 14:07:39.262000 print datetime.today() # 2015-04-21 14:08:20.362000 print datetime.now() # 1429596607.06 # 2015-04-21 14:10:07.061000 t = time.time() print t print datetime.fromtimestamp(t) from datetime import datetime, date, time a = date(2015, 4, 21) b = time(14, 13, 34) # 2015-04-21 14:13:34 print datetime.combine(a, b)
实例方法
datetime.date(): 返回相同年月日的date对象.
datetime.time(): 返回相同时分秒微秒的time对象.
datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], 与date类似.
其他方法可查看官方文档…
from datetime import datetime, date, time td = date(2015, 4, 21) n = time(14, 28, 30) # 2099-04-21 14:30:42.103000 print datetime.now(0.replace(year=2099)
类属性
datetime.min: datetime(MINYEAR, 1, 1).
datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).
实例属性(read-only)
datetime.year: 1 至 9999
datetime.month: 1 至 12
datetime.day: 1 至 n
datetime.hour: In range(24). 0 至 23
datetime.minute: In range(60).
datetime.second: In range(60).
datetime.microsecond: In range(1000000).
time类
time 代表本地(一天内)时间.
class datetime.time([hour [, minute [, second [, microsecond [, tzinfo]]]]]) # All arguments are optional. # 所有参数都是可选的. 0 <= hour < 24 0 <= minute < 60 0 <= second < 60 0 <= microsesond < 10**6
time类就是对时间的一些操作,其功能类似与datetime.其实date和time就是对datetime中日期和时间的操作.
更多Python中的time模块与datetime模块相关文章请关注PHP中文网!