亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PYTHON基礎-時間日期處理小結

發布時間:2020-08-28 12:09:32 來源:腳本之家 閱讀:167 作者:mdxy-dxy 欄目:開發技術

步驟:

1. 掌握幾種對象及其關系
2. 了解每類對象的基本操作方法
3. 通過轉化關系轉化

涉及對象

1. datetime

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2018, 1, 12, 23, 9, 12, 946118)
>>> type(now)
<type 'datetime.datetime'>

2. timestamp

>>> import time
>>> time.time()
1421075455.568243

3. time tuple

>>> import time
>>> time.localtime()
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=10, tm_sec=30, tm_wday=0, tm_yday=12, tm_isdst=0)

4. string

>>> import datetime
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2015-01-12 23:13:08'

5. date

>>> import datetime
>>> datetime.datetime.now().date()
datetime.date(2015, 1, 12)

datetime基本操作

1. 獲取當前datetime

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2015, 1, 12, 23, 26, 24, 475680)

2. 獲取當天date

>>> datetime.date.today()
datetime.date(2015, 1, 12)

3. 獲取明天/前N天

明天

>>> datetime.date.today() + datetime.timedelta(days=1)
datetime.date(2015, 1, 13)

三天前

>>> datetime.datetime.now()
datetime.datetime(2015, 1, 12, 23, 38, 55, 492226)
>>> datetime.datetime.now() - datetime.timedelta(days=3)
datetime.datetime(2015, 1, 9, 23, 38, 57, 59363)

4. 獲取當天開始和結束時間(00:00:00 23:59:59)

>>> datetime.datetime.combine(datetime.date.today(), datetime.time.min)
datetime.datetime(2015, 1, 12, 0, 0)
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.max)
datetime.datetime(2015, 1, 12, 23, 59, 59, 999999)

5. 獲取兩個datetime的時間差

>>> (datetime.datetime(2015,1,13,12,0,0) - datetime.datetime.now()).total_seconds()
44747.768075

6. 獲取本周/本月/上月最后一天

本周

>>> today = datetime.date.today()
>>> today
datetime.date(2015, 1, 12)
>>> sunday = today + datetime.timedelta(6 - today.weekday())
>>> sunday
datetime.date(2015, 1, 18)

本月

>>> import calendar
>>> today = datetime.date.today()
>>> _, last_day_num = calendar.monthrange(today.year, today.month)
>>> last_day = datetime.date(today.year, today.month, last_day_num)
>>> last_day
datetime.date(2015, 1, 31)

獲取上個月的最后一天(可能跨年)

>>> import datetime
>>> today = datetime.date.today()
>>> first = datetime.date(day=1, month=today.month, year=today.year)
>>> lastMonth = first - datetime.timedelta(days=1)

關系轉換

幾個關系之間的轉化

Datetime Object / String / timestamp / time tuple 

 關系轉換例子

datetime <=> string

datetime -> string 

>>> import datetime
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2015-01-12 23:13:08'

string -> datetime

>>> import datetime
>>> datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")
datetime.datetime(2014, 12, 31, 18, 20, 10)

datetime <=> timetuple

datetime -> timetuple

>>> import datetime
>>> datetime.datetime.now().timetuple()
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=17, tm_sec=59, tm_wday=0, tm_yday=12, tm_isdst=-1)

timetuple -> datetime

timetuple => timestamp => datetime [看后面datetime<=>timestamp]

datetime <=> date

datetime -> date

>>> import datetime
>>> datetime.datetime.now().date()
datetime.date(2015, 1, 12)

date -> datetime

>>> datetime.date.today()
datetime.date(2015, 1, 12)
>>> today = datetime.date.today()
>>> datetime.datetime.combine(today, datetime.time())
datetime.datetime(2015, 1, 12, 0, 0)
>>> datetime.datetime.combine(today, datetime.time.min)
datetime.datetime(2015, 1, 12, 0, 0)

datetime <=> timestamp

datetime -> timestamp

>>> now = datetime.datetime.now()
>>> timestamp = time.mktime(now.timetuple())
>>> timestamp
1421077403.0

timestamp -> datetime

>>> datetime.datetime.fromtimestamp(1421077403.0)
datetime.datetime(2015, 1, 12, 23, 43, 23)

這篇文章就介紹到這了,基本上都是python的關于日期時間操作的基礎知識點,需要掌握好

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宁强县| 三江| 莱州市| 哈密市| 金川县| 浦东新区| 白银市| 海阳市| 南乐县| 阿尔山市| 黄浦区| 晋江市| 灵丘县| 和硕县| 沅江市| 乌审旗| 贵州省| 保定市| 北碚区| 达拉特旗| 北海市| 巩留县| 卢湾区| 个旧市| 赤峰市| 洮南市| 临湘市| 邛崃市| 商南县| 高尔夫| 平定县| 柯坪县| 华亭县| 吴川市| 灌南县| 新巴尔虎左旗| 依安县| 吉木乃县| 米易县| 邯郸县| 兴山县|