Python基礎(chǔ)學(xué)習(xí)之時間轉(zhuǎn)換函數(shù)用法詳解
本文實例講述了Python基礎(chǔ)學(xué)習(xí)之時間轉(zhuǎn)換函數(shù)用法。分享給大家供大家參考,具體如下:
前言
python的時間格式分為多種,幾種格式之間的轉(zhuǎn)換方法時常是我們遇到的而且是經(jīng)常忘記的點,python不像php,時間字符串和datetime是一起的,只需要strtotime和date函數(shù)就可以相互轉(zhuǎn)化。雖然網(wǎng)上已經(jīng)有很多python時間轉(zhuǎn)換的文章,但是由于作者本人經(jīng)常做海外業(yè)務(wù),需要各種時區(qū)之間的轉(zhuǎn)換,所以這篇文章會對按時區(qū)轉(zhuǎn)換各種時間格式做一個總結(jié)。
轉(zhuǎn)換方法圖示(圖片轉(zhuǎn)自網(wǎng)絡(luò)):
一、字符串轉(zhuǎn)時間戳
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、按時區(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()))
二、時間戳轉(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、按時區(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、按時區(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、按時區(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)時間戳
1、默認(rèn):
import time def datetime_to_timestamp(date): return int(time.mktime(date.timetuple()))
2、按時區(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()))
六、時間戳轉(zhuǎn)datetime
1、默認(rèn):
import datetime def timestamp_to_datetime(time_stamp): return datetime.datetime.fromtimestamp(time_stamp)
2、按時區(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ù)計算的在線工具供大家使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結(jié)》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python單例模式之selenium driver實現(xiàn)單例
這篇文章主要介紹了python單例模式之selenium driver實現(xiàn)單例,使用裝飾器實現(xiàn)單例,文章基于python的相關(guān)資料實現(xiàn)主題,具有一的的參考價值,需要的朋友可以參考一下2022-03-03利用Python進(jìn)行時間序列數(shù)據(jù)分析與可視化的代碼示例
隨著時間序列數(shù)據(jù)在金融、氣象、生態(tài)等領(lǐng)域的廣泛應(yīng)用,利用Python進(jìn)行時間序列數(shù)據(jù)分析和可視化已成為重要的技能之一,本文將介紹如何使用Python進(jìn)行時間序列數(shù)據(jù)分析和可視化,并給出相應(yīng)的代碼示例,需要的朋友可以參考下2023-11-11Python實現(xiàn)字符串格式化的方法小結(jié)
本篇文章主要介紹了Python實現(xiàn)字符串格式化的方法小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02