python中時間模塊的基本使用教程
前言:
在開發(fā)中經(jīng)常會與時間打交道,如:獲取事件戳,時間戳的格式化等,這里簡要記錄一下python操作時間的方法。
python中常見的處理時間的模塊:
- time:處理時間的模塊,如獲取時間戳,格式化日期等
- datetime:date和time的結(jié)合體,處理日期和時間
- calendar:日歷相關(guān)的模塊,如:處理年歷/月歷
time模塊介紹
說明:time模塊主要講解如下內(nèi)容:
1.時間戳 --> 時間元組格式(time.struct_time) --> 日期字符串
2.日期字符串 --> 時間元組格式(time.struct_time) --> 時間戳
3.獲取當(dāng)前時間的分鐘/秒
4.獲取整分鐘/整小時時間戳
1.時間戳 --> 時間元組格式(time.struct_time) --> 日期字符串
時間戳 --> 時間元組格式
time.localtime(timestamp) # 參數(shù)timestamp為秒級時間戳
例子:
import time time_tuple = time.localtime(time.time()) print time_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=29, tm_sec=33, tm_wday=2, tm_yday=30, tm_isdst=0)
時間元組 --> 日期字符串
time.strftime(format, p_tuple=None):format:格式化的日期樣式;p_tuple:時間元組
例子:
time_format = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple) print time_format # 2019-01-30 11:48:07
封裝成方法:
def timestamp_format(timestamp): """ :brief 時間戳格式化 :param timestamp: 時間戳 :return: 格式化后的日期 """ return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
2.日期字符串 --> 時間元組格式(time.struct_time) --> 時間戳
日期字符串 --> 時間元組
time.strptime(string, format) # string:日期字符串,format:該日期字符串對應(yīng)的格式化格式
例子:
import time time_str_to_tuple = time.strptime("2019-01-30 11:48:07", "%Y-%m-%d %H:%M:%S") print time_str_to_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=48, tm_sec=7, tm_wday=2, tm_yday=30, tm_isdst=-1)
時間元組 --> 時間戳
time.mktime(p_tuple):p_tuple:時間元組
例子:
time_tuple_to_timestamp = int(time.mktime(time_str_to_tuple)) print time_tuple_to_timestamp # 結(jié)果:1548820087
封裝成方法
def time_str_to_timestamp(date_str, format): """ :brief 將字符串日期轉(zhuǎn)換為時間戳 :param date_str: 日期字符串,如:2019-01-30 11:48:07 :param format: 日期字符串對應(yīng)的格式化格式,如:%Y-%m-%d %H:%M:%S :return: 時間戳 """ return int(time.mktime(time.strptime(date_str, format)))
3.獲取當(dāng)前時間的分鐘/秒
獲取當(dāng)前時間戳
timestamp = int(time.time())
獲取當(dāng)前時間的秒
seconds = timestamp % 60 print "seconds:{}".format(seconds)
獲取當(dāng)前時間的分鐘
minute = (timestamp - seconds) % (60 * 60) print "minute:{}".format(minute / 60)
4.獲取整分鐘/整小時時間戳
思路:
先除以對應(yīng)的進制值取整,得到舍棄余數(shù)部分的整數(shù),然后再乘以對應(yīng)的進制值
one_minute = 60 # 一分鐘 one_hour = one_minute * 60 # 一小時 whole_minute = int(timestamp / one_minute) * one_minute whole_hour = int(timestamp / one_hour) * one_hour
datetime模塊介紹
datetime模塊中常見的類:
- datetime.date:處理日期
- datetime.time:處理時間
- datetime.datetime:處理日期和時間
- datetime.timedelta:處理時間差
說明:datetime模塊主要講解如下內(nèi)容
1.時間戳 --> datetime時間格式 --> 日期字符串
2.日期字符串 --> datetime時間格式 --> 時間元組格式(time.struct_time) --> 時間戳
3.時間差的使用,根據(jù)當(dāng)前時間獲取前N天的時間
1.時間戳 --> datetime時間格式 --> 日期字符串
時間戳 --> datetime時間格式
datetime.datetime.fromtimestamp(timestamp) 參數(shù)timestamp:時間戳
例子:
import time, datetime datetime_type = datetime.datetime.fromtimestamp(time.time()) print type(datetime_type) # <type 'datetime.datetime'>
datetime時間格式 --> 日期字符串
datetime.datetime.strftime(format) format:日期字符串對應(yīng)的格式化格式
例子:
datetime_format = datetime_type.strftime("%Y/%m/%d %H:%M:%S") print datetime_format # 2019/01/30 16:44:01
2.日期字符串 --> datetime時間格式 --> 時間元組格式(time.struct_time) --> 時間戳
日期字符串 --> datetime時間格式
datetime.datetime.strptime(date_str, format) date_str:字符串日期 format:日期字符串對應(yīng)的格式化格式
例子:
datetime_type = datetime.datetime.strptime('2019/01/30 16:44:01', '%Y/%m/%d %H:%M:%S') print type(datetime_type) # <type 'datetime.datetime'> # print datetime_type.timestamp() print time.mktime(datetime_type.timetuple())
datetime時間格式 --> 時間元組格式(time.struct_time) --> 時間戳
datetime.datetime.timetuple(): datetime轉(zhuǎn)換為時間元組
例子:
datetime_type_to_timestamp = int(time.mktime(datetime_type.timetuple())) print datetime_type_to_timestamp
3.時間差的使用,根據(jù)當(dāng)前時間獲取前N天的時間
datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
參數(shù)說明:
1.days:天
2.seconds:秒
3.microseconds:毫秒 1秒 = 10^3 毫秒
4.milliseconds:微秒 1秒 = 10^6 微秒
5.minutes,分鐘
6.hours:小時
7.weeks:星期 1weeks = 7days
例子:
day_timedelta = datetime.timedelta(days=1) # 獲取1天的時間值 forward_datetime = datetime.datetime.today() - day_timedelta # 獲取前一天的datetime值 print forward_datetime
calendar模塊介紹
說明:
這里介紹一下使用month(year, month)方法打印出某年某月下的月歷時間
例子:
import calendar cal = calendar.month(2019, 1) # 打印出2019年1月的月歷 print cal
喜歡點個贊!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
python連接數(shù)據(jù)庫后通過占位符添加數(shù)據(jù)
在pymysql中支持對占位符的處理,開發(fā)者需要在SQL中使用“%”定義占位符,在使用excute()方法執(zhí)行時對占位符的數(shù)據(jù)進行填充即可,本文給大家介紹python連接數(shù)據(jù)庫后通過占位符添加數(shù)據(jù)的方法,需要的朋友參考下吧2021-12-12python 獲取網(wǎng)頁編碼方式實現(xiàn)代碼
這篇文章主要介紹了python 獲取網(wǎng)頁編碼方式實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03解析Anaconda創(chuàng)建python虛擬環(huán)境的問題
這篇文章主要介紹了Anaconda創(chuàng)建python虛擬環(huán)境,包括虛擬環(huán)境管理、虛擬環(huán)境中python包管理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03