datetime 時間日期處理小結

原則, 以datetime爲中心, 起點或中轉, 轉化爲目標對象, 涵蓋了大多數業務場景中須要的日期轉換處理.python

  1. 掌握幾種對象及其關係
  2. 瞭解每類對象的基本操做方法
  3. 經過轉化關係轉化

涉及對象

1. datetime

import datetime

now = datetime.datetime.now()
print(now)  # 2018-12-26 18:05:32.983644
print(type(now))  # <class 'datetime.datetime'>
print(str(now))  # 2018-12-26 18:05:32.983644
print(str(now)[:19])  # 2018-12-26 18:05:32

2. timestamp

import time

t = time.time()
print(t)  # 1545818869.5371678
print(type(t))  # <class 'float'>

3. time tuple

import time

l = time.localtime()
print(l)
# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=26, tm_hour=18, tm_min=8, tm_sec=44, tm_wday=2, tm_yday=360, tm_isdst=0)
print(type(l))  # <class 'time.struct_time'>

4. string

import datetime

now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(now)  # 2018-12-26 18:09:56
print(type(now))  # <class 'str'>

5. date

import datetime

date = datetime.datetime.now().date()
print(date)  # 2018-12-26
print(type(date))  # <class 'datetime.date'>

datetime基本操做

1. 獲取當前datetime

import datetime
now = datetime.datetime.now()
print(now)
# datetime.datetime(2015, 1, 12, 23, 26, 24, 475680)

2. 獲取當天date

import datetime

today = datetime.date.today()

print(today)  # 2018-12-26
print(type(today))  # <class 'datetime.date'>

3. 獲取明天/前N天

明天code

import datetime

_time = datetime.date.today() + datetime.timedelta(days=1)
print(_time)  # 2018-12-27

三天前對象

import datetime

now = datetime.datetime.now()
print(now)  # 2018-12-26 18:14:37.221555
_time = datetime.datetime.now() - datetime.timedelta(days=3)
print(_time)  # 2018-12-23 18:14:37.221608

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

import datetime

min_time = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
print(min_time)  # 2018-12-26 00:00:00
max_time = datetime.datetime.combine(datetime.date.today(), datetime.time.max)
print(max_time)  # 2018-12-26 23:59:59.999999

5. 獲取兩個datetime的時間差

import datetime
from time import sleep

start = datetime.datetime.now()
sleep(3)
end = datetime.datetime.now()

print(end - start)  # 0:00:03.004487

6. 獲取本週/本月/上月最後一天

本週string

import datetime

today = datetime.date.today()
print(today) # 2018-12-26

sunday = today + datetime.timedelta(6 - today.weekday())
print(sunday) # 2018-12-30

本月ast

import calendar
import datetime

today = datetime.date.today()
_, last_day_num = calendar.monthrange(today.year, today.month)
last_day = datetime.date(today.year, today.month, last_day_num)

print(last_day)  # 2018-12-31

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

import datetime

today = datetime.date.today()
print(today)  # 2018-12-26
first = datetime.date(day=1, month=today.month, year=today.year)
print(first)  # 2018-12-01
lastMonth = first - datetime.timedelta(days=1)

print(f"上個月最後一天:{lastMonth}")  # 上個月最後一天:2018-11-30

關係轉換

幾個關係之間的轉化import

Datetime Object / String / timestamp / time tupledate

關係轉換例子

datetime -> string

import datetime

now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(now)  # 2018-12-26 18:24:58
print(type(now))  # <class 'str'>

string -> datetime

import datetime

now = datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")
print(now)  # 2014-12-31 18:20:10
print(type(now))  # <class 'datetime.datetime'>

datetime -> timetuple

import datetime

t = datetime.datetime.now().timetuple()
print(t)
# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=26, tm_hour=18, tm_min=26, tm_sec=36, tm_wday=2, tm_yday=360, tm_isdst=-1)
print(type(t))  # <class 'time.struct_time'>

datetime -> date

import datetime

date = datetime.datetime.now().date()
print(date)  # 2018-12-26
print(type(date))  # <class 'datetime.date'>

date -> datetime

import datetime

today = datetime.date.today()
print(today)  # 2018-12-26

date_time = datetime.datetime.combine(today, datetime.time())
print(date_time)  # 2018-12-26 00:00:00

datetime -> timestamp

import datetime
import time

now = datetime.datetime.now()
timestamp = time.mktime(now.timetuple())
print(timestamp)  # 1545820225.0

timestamp -> datetime

import datetime

f_timestamp = datetime.datetime.fromtimestamp(1421077403.0)
print(f_timestamp)  # 2015-01-12 23:43:23
print(type(f_timestamp))  # <class 'datetime.datetime'>
相關文章
相關標籤/搜索