99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python中的time模塊與datetime模塊用法總結(jié)
Python中的time模塊與datetime模塊用法總結(jié)
2018-07-12
收藏

Python中的time模塊與datetime模塊用法總結(jié)

Python中內(nèi)置的各項時間日期函數(shù)幾乎都來自于time和datetime這兩個模塊,下面整理了Python中的time模塊與datetime模塊用法總結(jié),需要的朋友可以參考下

time模塊
time模塊是包含各方面對時間操作的函數(shù). 盡管這些常常有效但不是所有方法在任意平臺中有效. 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

函數(shù)

    time.time(): 返回一個時間戳
    time.asctime([t]): 轉(zhuǎn)換gmtime()和localtime()返回的元組或struct_time為string.
    time.clock(): 在第一次調(diào)用的時候, 返回程序運行的時間. 第二次之后返回與之前的間隔.
    time.ctime([secs]): 將時間戳轉(zhuǎn)換為時間字符串, 如沒有提供則返回當(dāng)前的時間字符串,并與asctime(localtime())一樣.
    time.gmtime([secs]): 將時間戳轉(zhuǎn)化為, UTC 時區(qū)的struct_time.
    time.localtime([secs]): 類似gmtime()但會把他轉(zhuǎn)換成本地時區(qū).
    time.mktime(t): struct_time 轉(zhuǎn)化為時間戳.
    time.sleep(secs): 線程推遲指定時間, 以秒為單位.
    time.strftime(format[,t]): 根據(jù)參數(shù)轉(zhuǎn)換一個sturc_time或元組為字符串.
    time.strptime(string[, format]): 與strftime相反,返回一個struct_time.    
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模塊中常用的格式化字符串

    %y 兩位數(shù)的年份 00 ~ 99.
    %Y 四位數(shù)的年份 0000 ~ 9999
    %m 月份 01 ~ 12.
    %d day 01 ~ 31.
    %H 時 00 ~ 23.
    %I 時 01 ~ 12.
    %M 分 00 ~ 59.
    %S 秒 00 ~ 61.

datetime模塊
datetime模塊提供對于日期和時間進行簡單或復(fù)雜的操作. datetime 模塊提供了一下的可用類型(Available Types).

datetime.MINYEAR 和 datetime.MAXYEAR 模塊常量表示datetime接受的范圍

    class datetime.date: 一個理想化的日期, 提供year, month, day屬性
    class datetime.time: 一個理想化的時間, 提供hour, minute, second, microsecond, tzinfo.
    class datetime.datetime: 日期和時間的組合.提供year, month, day, hour, minute, second, microsecond, tzinfo.
    class datetime.timedelta: 表達兩個date,time和datetime持續(xù)時間內(nèi)的微妙差異.
    class datetime.tzinfo: 時間對象的抽象基類.    
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.
  # 所有參數(shù)都是必須的. 參數(shù)可能是 int 或 long.
  MINYEAR <= year <= MAXYEAR
  1<= month <= 12
  1<= day <= number of days in the given month and year.(隨著月份和年份)

如果參數(shù)脫離給的范圍會拋出, valueError.

1.類方法 >`date.today()`:返回當(dāng)前的本地日期, 這等價于 `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):根據(jù)提供的時間戳返回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):根據(jù)提供的Gregorian日歷返回date.(不做描述)

類屬性

    date.min: 返回 date(MINYEAR, 1, 1).
    date.max: 返回 date(MAXYEAR, 12, 31).
    date.year: 返回 年, MINYEAR和MAXYEAR之間
    date.month: 返回 月, 1到12月之間
    date.day: 返回 1到 n 之間.    
d = date(2014, 4, 21)
# 2014 4 21
print d.year, d.month, d.day

實例方法

    date.replace(year, month, day):返回一個相同值的data對象, 除了這些參數(shù)給關(guān)鍵字指定新的值.
    date.timetuple(): 返回一個time.struct_time對象.
    date.toordinal(): 返回一個Gregoian Calendar對象.
    date.weekday(): 返回day of the week. 星期一為0,星期日為6.
    date.isoweekday(): 返回day of the week. 星期一為1,星期日為7.
    date.isocalendar(): 返回一個三元組, (ISO year, ISO week number, ISO weekday).
    date.isoformat(): 返回 一個'YYYY-MM-DD'的字符串格式.
    date.ctime(): 返回一個字符串日期, d.ctime() 等同于 time.ctime(time.mktime(d.timetuple())).
    date.strftime(format): 返回一個字符串日期, 格式自定義.    
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.today(): 返回當(dāng)前本地datetime.隨著 tzinfo None. 這個等同于datetime.fromtimestamp(time.time()).
    datetime.now([tz]): 返回當(dāng)前本地日期和時間, 如果可選參數(shù)tz為None或沒有詳細說明,這個方法會像today().
    datetime.utcnow(): 返回當(dāng)前的UTC日期和時間, 如果tzinfo None ,那么與now()類似.
    datetime.fromtimestamp(timestamp[, tz]): 根據(jù)時間戳返回本地的日期和時間.tz指定時區(qū).
    datetime.utcfromtimestamp(timestamp): 根據(jù)時間戳返回 UTC datetime.
    datetime.fromordinal(ordinal): 根據(jù)Gregorian ordinal 返回datetime.
    datetime.combine(date, time): 根據(jù)date和time返回一個新的datetime.
    datetime.strptime(date_string, format): 根據(jù)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 代表本地(一天內(nèi))時間.
class datetime.time([hour
          [, minute
          [, second
          [, microsecond
          [, tzinfo]]]]])
  # All arguments are optional.
  # 所有參數(shù)都是可選的.
  0 <= hour < 24
  0 <= minute < 60
  0 <= second < 60
  0 <= microsesond < 10**6
time類就是對時間的一些操作,其功能類似與datetime.其實date和time就是對datetime中日期和時間的操作.

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }