亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解python日期時(shí)間處理2

 更新時(shí)間:2021年12月15日 15:05:53   作者:雷學(xué)委  
這篇文章主要為大家介紹了python日期時(shí)間處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

前篇我們稍微學(xué)習(xí)了Python中時(shí)間的獲取,這次繼續(xù)學(xué)習(xí)日期的時(shí)區(qū)轉(zhuǎn)換,格式化等等。

開(kāi)發(fā)中常用的日期操作還有哪些?

  • 時(shí)區(qū)轉(zhuǎn)換顯示
  • 日期格式化
  • 秒數(shù) 與 日期 與 字符串的轉(zhuǎn)換

我們經(jīng)常會(huì)用到,比如全球化的業(yè)務(wù)根據(jù)不同客戶顯示不同時(shí)間(格式等)

在python 主要有下面兩個(gè)模塊涵蓋了常用日期處理

import time
import calender

我們看看這兩個(gè)模塊。

時(shí)間處理中的類型轉(zhuǎn)換:struct_time vs str

Python中創(chuàng)建一個(gè)時(shí)間,具體來(lái)說(shuō)創(chuàng)建一個(gè)struct_time 需要一個(gè)9個(gè)元素的元組來(lái)構(gòu)造。

asctime 函數(shù)幫我們把這種類型的時(shí)間格式化為字符串。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello

import time
# fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
the9fields = (2021, 11, 10, 22, 55, 11, 16, 16, 16)
fixed = time.struct_time(the9fields)
print("fixed time:", fixed)
print("type:", type(fixed))
result = time.asctime(the9fields)  # 類似struct_time,需要9個(gè)元素構(gòu)成的元組參數(shù)。
print("asc time:", result)
print("type:", type(result))
localtime = time.localtime()
print("local time:", localtime)
print("type:", type(localtime))
print("asc time:", time.asctime(localtime))

運(yùn)行效果如下:

屏幕快照 2021-11-12 上午12.14.14.png

這個(gè)ticks就是從0時(shí)刻計(jì)算,至今的秒數(shù)累計(jì)。

可以隔一秒運(yùn)行這個(gè)程序,每次ticks值加上1(近似)

指定輸入來(lái)構(gòu)造時(shí)間:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello

import time
#fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
fixed = time.struct_time((2021, 11, 10, 22, 55, 11, 16, 16, 16))
print("fixed time:", fixed)

運(yùn)行效果如下:

時(shí)間與字符串轉(zhuǎn)換

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @XueWeiTag: CodingDemo
# @File : createtime2.py
# @Project : hello

import time
sec = 3600  # 紀(jì)元開(kāi)始后的一個(gè)小時(shí)(GMT 19700101凌晨)
#
gmtime = time.gmtime(sec)
print("gmtime:", gmtime)  # GMT
print("type:", type(gmtime))
print(time.strftime("%b %d %Y %H:%M:%S", gmtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime))  # 打印日期加上時(shí)區(qū)
print("*" * 16)
localtime = time.localtime(sec)
print("localtime:", localtime)  # 本地時(shí)間
print("type:", type(localtime))
print(time.strftime("%b %d %Y %H:%M:%S", localtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime))  # 打印日期加上時(shí)區(qū)
#試試其他格式
print(time.strftime("%D", localtime))
print(time.strftime("%T", localtime))

下面是運(yùn)行結(jié)果:

對(duì)于時(shí)間格式化函數(shù)(strftime) 它并不理會(huì)你傳入的時(shí)間(struct_time)是哪個(gè)時(shí)區(qū)的,照樣給你輸出,也是正確的。

但是我們寫程序拿數(shù)據(jù)的時(shí)候,必須把時(shí)區(qū)信息原樣返回到用戶端,或者是UI端,最后由客戶端本地時(shí)區(qū)設(shè)置進(jìn)行調(diào)整顯示。

最后看看,日期文本轉(zhuǎn)換為日期(struct_time).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 7:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @XueWeiTag: CodingDemo
# @File : createtime4.py
# @Project : hello
import time
print("strptime1:", time.strptime("Jan 01 1970 09:00:00", "%b %d %Y %H:%M:%S"))
print("strptime2:", time.strptime("1970-01-01 09:00:00", "%Y-%m-%d %H:%M:%S"))
print("strptime3:", time.strptime("1970-01-01 09:00:00 CST", "%Y-%m-%d %H:%M:%S %Z"))

下面是運(yùn)行結(jié)果:

總結(jié)

Python 日期處理挺多把戲的,換個(gè)格式打印/轉(zhuǎn)換,結(jié)果就不一樣了。

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評(píng)論