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

熱線電話:13121318867

登錄
首頁精彩閱讀Python 日期和時間_python 當(dāng)前日期時間_python日期格式化
Python 日期和時間_python 當(dāng)前日期時間_python日期格式化
2016-12-31
收藏

Python 日期和時間_python 當(dāng)前日期時間_python日期格式化

Python程序能用很多方式處理日期和時間,轉(zhuǎn)換日期格式是一個常見的功能。

Python 提供了一個 time 和 calendar 模塊可以用于格式化日期和時間。

時間間隔是以秒為單位的浮點小數(shù)。

每個時間戳都以自從1970年1月1日午夜(歷元)經(jīng)過了多長時間來表示。

Python 的 time 模塊下有很多函數(shù)可以轉(zhuǎn)換常見日期格式。如函數(shù)time.time()用于獲取當(dāng)前時間戳, 如下實例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time;  # 引入time模塊

ticks = time.time()
print "當(dāng)前時間戳為:", ticks
以上實例輸出結(jié)果:

當(dāng)前時間戳為: 1459994552.51
時間戳單位最適于做日期運算。但是1970年之前的日期就無法以此表示了。太遙遠(yuǎn)的日期也不行,UNIX和Windows只支持到2038年。

什么是時間元組?

很多Python函數(shù)用一個元組裝起來的9組數(shù)字處理時間:

上述也就是struct_time元組。這種結(jié)構(gòu)具有如下屬性:

 
獲取當(dāng)前時間

從返回浮點數(shù)的時間輟方式向時間元組轉(zhuǎn)換,只要將浮點數(shù)傳遞給如localtime之類的函數(shù)。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time

localtime = time.localtime(time.time())
print "本地時間為 :", localtime
以上實例輸出結(jié)果:

本地時間為 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)
 
獲取格式化的時間

你可以根據(jù)需求選取各種格式,但是最簡單的獲取可讀的時間模式的函數(shù)是asctime():

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time

localtime = time.asctime( time.localtime(time.time()) )
print "本地時間為 :", localtime
以上實例輸出結(jié)果:

本地時間為 : Thu Apr  7 10:05:21 2016
格式化日期

我們可以使用 time 模塊的 strftime 方法來格式化日期,:

time.strftime(format[, t])
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time

# 格式化成2016-03-20 11:45:39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

# 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
 
# 將格式字符串轉(zhuǎn)換為時間戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
以上實例輸出結(jié)果:

2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0
python中時間日期格式化符號:

%y 兩位數(shù)的年份表示(00-99)
%Y 四位數(shù)的年份表示(000-9999)
%m 月份(01-12)
%d 月內(nèi)中的一天(0-31)
%H 24小時制小時數(shù)(0-23)
%I 12小時制小時數(shù)(01-12)
%M 分鐘數(shù)(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應(yīng)的日期表示和時間表示
%j 年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(shù)(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(shù)(00-53)星期一為星期的開始
%x 本地相應(yīng)的日期表示
%X 本地相應(yīng)的時間表示
%Z 當(dāng)前時區(qū)的名稱
%% %號本身
獲取某月日歷

Calendar模塊有很廣泛的方法用來處理年歷和月歷,例如打印某月的月歷:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import calendar

cal = calendar.month(2016, 1)
print "以下輸出2016年1月份的日歷:"
print cal;
以上實例輸出結(jié)果:

以下輸出2016年1月份的日歷:
    January 2016
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 31
 
Time 模塊

Time 模塊包含了以下內(nèi)置函數(shù),既有時間處理相的,也有轉(zhuǎn)換時間格式的:

Time模塊包含了以下2個非常重要的屬性:

日歷(Calendar)模塊

此模塊的函數(shù)都是日歷相關(guān)的,例如打印某月的字符月歷。

星期一是默認(rèn)的每周第一天,星期天是默認(rèn)的最后一天。更改設(shè)置需調(diào)用calendar.setfirstweekday()函數(shù)。模塊包含了以下內(nèi)置函數(shù):
 
其他相關(guān)模塊和函數(shù)
Python中,其他處理日期和時間的模塊還有:
datetime模塊
pytz模塊
dateutil模塊

數(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 進(jìn)行初始化 // 參數(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); }