目錄
前篇我們稍微學習了python中時間的獲取,這次繼續學習日期的時區轉換,格式化等等。
我們經常會用到,比如全球化的業務根據不同客戶顯示不同時間(格式等)
在python 主要有下面兩個模組涵蓋了常用日期處理
import time
import calender
python中建立乙個時間,具體來說建立乙個struct_time 需要乙個9個元素的元組來構造。
asctime 函式幫我們把這種型別的時間格式化為字串。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2021/11/10 22:49 上午
# @auth程式設計客棧or : leixuewei
# @csdn/juejin/wechat: 雷學委
# @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個元素構成的元組引數。
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))
執行效果如下:
這個ticks就是從0時刻計算,至今的秒數累計。
可以隔一秒執行這個程式,每次ticks值加上1(近似)
指定輸入來構造時間:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2021/11/10 22:49 上午
# @author : leixuewei
# @csdn/juejin/wechat: 雷學委
# @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)
執行效果如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2021/11/10 22:49 上午
# @author : leixuewei
# @csdn/juejin/wechat: 雷學委
# @xueweitag: codingdemo
# @fi : createtime2.py
# @project : hello
import time
sec = 3600 # 紀元開始後的乙個小時(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)) # 列印日期加上時區
print("*" * 16)
localtime = time.localtime(sec)
print("localtime:", localtime) # 本地時間
print("type:", type(localtiecuplme))
print(time.strftime("%b %d %y %h:%m:%s", localtime))
print(time.strftime("%y-%m-%d %h:%m:%s %z", localtime)) # 列印日期加上時區
#試試其他格式
print(time.strftime("%d", localtime))
print(time.strftime("%t", localtime))
下面是執行結果:
對於時間格式化函式(strftime) 它並不理會你傳入的時間(struct_time)是哪個時區的,照樣給你輸出,也是正確的。
但是我們寫程式拿資料的時候,必須把時區資訊原樣返回到使用者端,或者是ui端,最後由客戶端本地時區設定進行調整顯示。
最後看看,日期文字轉換為日期(struct_time).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time : 2021/11/10 7:49 上午
# @author : leixuewei
# @csdn/juejin/wechat: 雷學委
# @xueweitag: codingdemo
# @file : createtime4.py
# @project : hello
import time
print("strptime1:", time.strptime("jwww.cppcns.coman 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"))
下面是執行結果:
python 日期處理挺多把戲的,換個格式列印/轉換,結果就不一樣了。
本文標題: 詳解python日期時間處理2
本文位址:
python 日期處理 python 日期時間處理
獲取日期 import datetime 呼叫事件模組 today datetime.date.today 獲取今天日期 deltadays datetime.timedelta days 1 確定日期差額,如前天 days 2 yesterday today deltadays 獲取差額日期,昨天...
Python 日期時間處理
所有日期 時間的api都在datetime模組內。1.日期輸出格式化 datetime string import datetime now datetime.datetime.now now.strftime y m d h m s 輸出 2015 04 07 19 11 21 strftime是...
python 日期時間處理
獲取日期 import datetime 呼叫事件模組 today datetime.date.today 獲取今天日期 deltadays datetime.timedelta days 1 確定日期差額,如前天 days 2 yesterday today deltadays 獲取差額日期,昨天...