1.包的导入
功能: 将python中的某个包或模块,导入到当前的py文件中 用法: import package 参数: package:被导入的包的名字 要求: 只会拿到对应包下__init__中的功能或当前模块下的功能
2.模块的导入
模块的导入 from .. import .. 功能: 通过从某个包中找到对应的模块 用法: from package import module 参数: package:来源的包名 module:包中的目标模块
1.什么是第三方包?
2. ipython包
3. datetime包
导入包与模块: from datetime import datetime import datetime 使用方法: datetime.now() datetime.datetime.now() (today)
# 获取时间间隔 导入包: from datetime import datetime from datetime import timedelta 使用方法: timeobj = timedalta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, week=0)
# 时间对象可以做加减 from datetime import datetime from datetime import timedelta now = datetime.now() print(now, type(now)) three_days = timedelta(days=3) after_three_day = now + three_days print(after_three_day) before_three_day = now - three_days print(before_three_day) one_hour = timedelta(hours=1) before_one_hour = now - one_hour print(before_one_hour)
运行结果:
将日期对象转为字符串
获取时间对象: from datetime import datetime now = datetime.now() 时间转字符串: date_str = now.strftime(format)
from datetime import datetime from datetime import timedelta now = datetime.now() print(now, type(now)) now_str = now.strftime('%Y-%m-%H %H:%M:%S') print(now_str, type(now_str)) three_days = timedelta(days=3) after_three_day = now + three_days print(after_three_day) after_three_day_str = after_three_day.strftime('%Y/%m/%H %H:%M:%S') print(after_three_day_str, type(after_three_day_str)) before_three_day = now - three_days print(before_three_day) before_three_day_str = before_three_day.strftime('%Y%m%d') print(before_three_day_str, type(before_three_day_str)) one_hour = timedelta(hours=1) before_one_hour = now - one_hour print(before_one_hour) before_one_hour_str = before_one_hour.strftime('%Y') print(before_one_hour_str, type(before_three_day_str))
时间字符串转时间类型
获取时间模块: from datetime import datetime 时间转字符串: datetime.strptime(tt, format) 参数介绍: tt: 符合时间格式的字符串 format:tt时间字符串匹配规则
from datetime import datetime from datetime import timedelta now = datetime.now() print(now, type(now)) now_str = now.strftime('%Y-%m-%H %H:%M:%S') print(now_str, type(now_str)) now_obj = datetime.strptime(now_str, '%Y-%m-%d %H:%M:%S') print(now_obj, type(now_obj), '-------------------') three_days = timedelta(days=3) after_three_day = now + three_days print(after_three_day) after_three_day_str = after_three_day.strftime('%Y-%m-%d %H:%M:%S') print(after_three_day_str, type(after_three_day_str)) after_three_day_obj = datetime.strptime(after_three_day_str, '%Y-%m-%d %H:%M:%S') print(after_three_day_obj, type(after_three_day_obj), '---------------------------') before_three_day = now - three_days print(before_three_day) before_three_day_str = before_three_day.strftime('%Y%m%d') print(before_three_day_str, type(before_three_day_str)) before_three_day_obj = datetime.strptime(before_three_day_str, '%Y%m%d') print(before_three_day_obj, type(before_three_day_obj), '--------------------------') one_hour = timedelta(hours=1) before_one_hour = now - one_hour print(before_one_hour) before_one_hour_str = before_one_hour.strftime('%Y') print(before_one_hour_str, type(before_three_day_str)) before_one_hour_obj = datetime.strptime(before_one_hour_str, '%Y') print(before_one_hour_obj, type(before_one_hour_obj), '----------------------------')
字符 | 介绍 |
---|---|
%Y | 完成的年份,如2023 |
%m | 月份,1-12 |
%d | 月中的某一天(1-31) |
%H | 一天中的第几个小时(24小时,00-23) |
字符 | 介绍 |
---|---|
%I | 一天中的第几个小时(12小时,01-12) |
%M | 当前的第几部分(00-59) |
%S | 当前分的第几秒(0-61)闰年多占2秒 |
%f | 当前秒的第多少毫秒 |
字符 | 介绍 |
---|---|
%a | 简化的星期,如星期三 Wed |
%A | 完整的星期,如星期三 Wednesday |
%b | 简化的月份,如二月份Feb |
%B | 完整的月份,如二月份February |
字符 | 介绍 |
---|---|
%c | 本地日期和时间,如 Wed Feb 5 10:14:30 2023 |
%p | 显示上午还是下午,如AM代表上午,PM代表下午 |
%j | 一年中的第几天 |
%U | 一年中的星期数 |