本文實(shí)例講述了Python基礎(chǔ)學(xué)習(xí)之時(shí)間轉(zhuǎn)換函數(shù)用法。分享給大家供大家參考,具體如下:
前言
python的時(shí)間格式分為多種,幾種格式之間的轉(zhuǎn)換方法時(shí)常是我們遇到的而且是經(jīng)常忘記的點(diǎn),python不像php,時(shí)間字符串和datetime是一起的,只需要strtotime和date函數(shù)就可以相互轉(zhuǎn)化。雖然網(wǎng)上已經(jīng)有很多python時(shí)間轉(zhuǎn)換的文章,但是由于作者本人經(jīng)常做海外業(yè)務(wù),需要各種時(shí)區(qū)之間的轉(zhuǎn)換,所以這篇文章會(huì)對按時(shí)區(qū)轉(zhuǎn)換各種時(shí)間格式做一個(gè)總結(jié)。
轉(zhuǎn)換方法圖示(圖片轉(zhuǎn)自網(wǎng)絡(luò)):
一、字符串轉(zhuǎn)時(shí)間戳
1、默認(rèn):
import time
def time_str_to_timestamp(string_time, _format="%Y-%m-%d %H:%M:%S"):
return int(time.mktime(time.strptime(string_time, _format)))
2、按時(shí)區(qū)轉(zhuǎn):
import time
import datetime
from pytz import timezone as tz
def time_str_to_timestamp_by_timezone(string_time, _format="%Y-%m-%d %H:%M:%S”, from_tz=“UTC”, to_tz="America/Los_Angeles"):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
return int(time.mktime(
datetime.datetime.strptime(string_time, _format).replace(
tzinfo=from_tz).astimezone(to_tz).timetuple()))
二、時(shí)間戳轉(zhuǎn)字符串
1、默認(rèn):
import time
def timestamp_to_str(timestamp, _format="%Y-%m-%d %H:%M:%S"):
return time.strftime(_format, time.localtime(timestamp))
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def timestamp_to_str_by_timezone(timestamp, _format="%Y-%m-%d %H:%M:%S”, to_tz="America/Los_Angeles"):
to_tz = tz(to_tz)
return str(datetime.datetime.fromtimestamp(timestamp, to_tz).strftime(_format))
三、字符串轉(zhuǎn)datetime
1、默認(rèn):
import datetime
def datetime_str_to_datetime(string_time, _format="%Y-%m-%d %H:%M:%S"):
return datetime.datetime.strptime(string_time, _format)
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def datetime_str_to_datetime_by_timezone(string_time, from_tz=“UTC”, to_tz="America/Los_Angeles”, _format="%Y-%m-%d %H:%M:%S",):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
return datetime.datetime.strptime(string_time, _format).replace(
tzinfo=from_tz).astimezone(to_tz)
四、datetime轉(zhuǎn)字符串
1、默認(rèn):
import datetime
def datetime_to_datetime_str(date, _format="%Y-%m-%d %H:%M:%S"):
return date.strftime(_format)
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def datetime_to_datetime_str_by_timezone(date, from_tz=“UTC”, to_tz="America/Los_Angeles”, _format="%Y-%m-%d %H:%M:%S"):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
date = date.replace(tzinfo=from_tz).astimezone(to_tz)
return date.strftime(_format)
五、datetime轉(zhuǎn)時(shí)間戳
1、默認(rèn):
import time
def datetime_to_timestamp(date):
return int(time.mktime(date.timetuple()))
2、按時(shí)區(qū)轉(zhuǎn):
import time
from pytz import timezone as tz
def datetime_to_timestamp_by_timezone(date, from_tz=“UTC”, to_tz="America/Los_Angeles"):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
return int(time.mktime(date.replace(
tzinfo=from_tz).astimezone(to_tz).timetuple()))
六、時(shí)間戳轉(zhuǎn)datetime
1、默認(rèn):
import datetime
def timestamp_to_datetime(time_stamp):
return datetime.datetime.fromtimestamp(time_stamp)
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def timestamp_to_datetime_by_timezone(time_stamp, to_tz="America/Los_Angeles"):
to_tz = tz(to_tz)
return datetime.datetime.fromtimestamp(time_stamp, to_tz)
PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計(jì)算的在線工具供大家使用:
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時(shí)間操作技巧總結(jié)》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

