1.字符串===》datetime

dd = '2019-03-17 11:00:00'
dd = datetime.datetime.strptime(dd, "%Y-%m-%d %H:%M:%S")

2.datetime====>字符串

dc = dd.strftime("%Y-%m-%d %H:%M:%S")

3.判断now是否是日期类型

isinstance(now, datetime.datetime)

4.1获取N天以后的日期

import time
import datetime
#先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() + datetime.timedelta(days = 3))
#转换为时间戳:
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
#转换为其他字符串格式:
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")

4.2获取多少天以后的日期

# 这个包可以传参数年月日都可以
from datetime import datetime
now_time = datetime.now()
#先安装这个包
# pip install python-dateutil
from dateutil.relativedelta import relativedelta as rd
result_date = now_time - rd(years=1)

 

5.时间戳&时间元组&字符串 的爱恨情仇

import time,datetime
# 字符串-->时间元组-->时间戳
# 字符串:
str_date = "2021-06-30"
# 时间元组 = fun(字符串)
arr_date = time.strptime(str_date,"%Y-%m-%d")
# 时间戳 = fun(时间元组)
nums_date = int(time.mktime(arr_date))#默认是float


import time,datetime
# 时间戳-->时间元组-->字符串
#1.获取时间戳
nums_date = time.time()
# 2.时间元组 = fun(时间戳)
arr_date = time.localtime(nums_date)
#3.字符串 = fun(时间元组)
str_date = time.strftime("%Y-%m-%d", arr_date)

6总结一下格式化用到的代码把

"%Y-%m-%d %H:%M:%S"

 7.跟雷子总结的,竟然还要这个玩意,这是毫秒级别的嘛

datetime.datetime.now().strftime(r"%Y%m%d%H%M%S%f") -----------------------------------------------------------------------------------------------------------------------------------------