涉及对象

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天

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. 获取本周/本月/上月最后一天

import datetime

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

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

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
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

关系转换

关系转换例子

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'>
12-26 22:15