Python基礎(chǔ)學(xué)習(xí)之時(shí)間轉(zhuǎn)換函數(shù)用法詳解
本文實(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ì)對(duì)按時(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)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python單例模式之selenium driver實(shí)現(xiàn)單例
這篇文章主要介紹了python單例模式之selenium driver實(shí)現(xiàn)單例,使用裝飾器實(shí)現(xiàn)單例,文章基于python的相關(guān)資料實(shí)現(xiàn)主題,具有一的的參考價(jià)值,需要的朋友可以參考一下2022-03-03Python對(duì)切片命名的實(shí)現(xiàn)方法
在本篇文章里我們給大家分享了關(guān)于Python對(duì)切片命名的實(shí)現(xiàn)方法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。2018-10-10使用PyOpenGL繪制三維坐標(biāo)系實(shí)例
今天小編就為大家分享一篇使用PyOpenGL繪制三維坐標(biāo)系實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12利用Python進(jìn)行時(shí)間序列數(shù)據(jù)分析與可視化的代碼示例
隨著時(shí)間序列數(shù)據(jù)在金融、氣象、生態(tài)等領(lǐng)域的廣泛應(yīng)用,利用Python進(jìn)行時(shí)間序列數(shù)據(jù)分析和可視化已成為重要的技能之一,本文將介紹如何使用Python進(jìn)行時(shí)間序列數(shù)據(jù)分析和可視化,并給出相應(yīng)的代碼示例,需要的朋友可以參考下2023-11-11Python實(shí)現(xiàn)字符串格式化的方法小結(jié)
本篇文章主要介紹了Python實(shí)現(xiàn)字符串格式化的方法小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02python3使用print打印帶顏色的字符串代碼實(shí)例
這篇文章主要介紹了python3使用print打印帶顏色的字符串代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08