【Python基础】时间和日期模块
【Python基础】时间和日期模块
Python中的time和datetime用法整理。
一、Python中时间的四种类型
- 时间戳 float
- 时间元组 struct_time
- 时间字符串 string
- 时间对象 datetime,date,time
二、time模块
python
# 导入包
import time
1. 时间类型
以时间戳(秒)形式,返回当前时间
python
time.time()
# 输出:1552884340.400742
以时间元组形式 struct_time,返回本地时间
python
time.localtime()
# 输出:time.struct_time(tm_year=2019, tm_mon=3, tm_mday=18, tm_hour=12, tm_min=47, tm_sec=18, tm_wday=0, tm_yday=77, tm_isdst=0)
以字符串形式,返回格式化的时间
python
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 输出:'2019-03-18 12:48:50'
格式化指定时间戳,输出字符串
python
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1554197885.739947))
# 输出:'2019-04-02 17:38:05'
2. 类型转换
时间元组 → 字符串
python
time.asctime(time.localtime())
# 输出:'Mon Mar 18 13:02:31 2019'
时间戳 → 字符串
python
time.ctime(1552884340.400742)
# 输出:'Mon Mar 18 12:45:40 2019'
time.ctime(time.time())
# 输出:'Mon Mar 18 13:03:32 2019'
时间元组 → 时间戳
python
time.mktime(time.localtime())
# 输出:1552885472.0
字符串 → 时间元组
python
time_str=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
# 输出:time.struct_time(tm_year=2019, tm_mon=3, tm_mday=18, tm_hour=13, tm_min=5, tm_sec=48, tm_wday=0, tm_yday=77, tm_isdst=-1)
三、datetime模块
python
# 导入包
import datetime
1. datetime时间的计算
datetime获取的是datetime对象
获取当前日期时间
python
datetime.datetime.now()
# 输出:datetime.datetime(2019, 3, 18, 15, 50, 31, 201287)
获取当天日期 date
python
datetime.date.today()
# 或
datetime.datetime.now().date()
# 输出:datetime.date(2019, 3, 18)
获取当前的时间 time
python
datetime.datetime.now().time()
# 输出:datetime.time(15, 58, 25, 658748)
获取时间差
可以计算相差多少 天(days), 秒(seconds), 微秒(microseconds)
python
start_time = datetime.datetime.now()
end_time = datetime.datetime.now()
(end_time - start_time).seconds
# 执行完第3行输出:6
获取当前时间向后8个小时的时间
可以计算:天(days),小时(hours),分钟(minutes),秒(seconds),微秒(microseconds)
python
datetime.datetime.now() + datetime.timedelta(hours=8)
# 输出:datetime.datetime(2019, 3, 19, 0, 19, 10, 233485)
获取当前是星期几
返回的1~7代表周一到周日,如果用的是weekday则返回的0~6代表周一到周日
python
datetime.date.today().isoweekday()
# 或
datetime.datetime.now().isoweekday()
# 输出:1
获取上周日和上周一的时间
python
today = datetime.date.today()
today_weekday = today.isoweekday()
last_sunday = today - datetime.timedelta(days=today_weekday)
last_monday = last_sunday - datetime.timedelta(days=6)
# 执行完第1行得到:today: datetime.date(2019, 3, 18)
# 执行完第3行得到:last_sunday: datetime.date(2019, 3, 17)
# 执行完第4行得到:last_monday: datetime.date(2019, 3, 11)
获取上个月第一天和最后一天的日期
python
today = datetime.date.today()
last_month_end = datetime.date(today.year, today.month, 1) - datetime.timedelta(days=1)
last_month_begin = datetime.date(last_month_end.year, last_month_end.month, 1)
# 执行完第1行得到:today: datetime.date(2019, 3, 18)
# 执行完第2行得到:last_month_end: datetime.date(2019, 2, 28)
# 执行完第3行得到:last_month_begin: datetime.date(2019, 2, 1)
计算指定日期当月最后一天的日期和本月天数
python
import datetime
def specified_date_info(date_object):
if date_object.month == 12:
next_month_first_date = datetime.date(date_object.year+1,1,1)
else:
next_month_first_date = datetime.date(date_object.year, date_object.month+1, 1)
return next_month_first_date - datetime.timedelta(1)
if __name__ == "__main__":
date = datetime.date(2018, 12, 25)
specified_month_last_date = specified_date_info(date)
specified_month_days = specified_date_info(date).day
print(specified_month_last_date)
print(specified_month_days)
# 输出:2018-12-31
# 输出:31
2. datetime时间类型的转换
时间戳 → datetime对象
python
datetime.datetime.fromtimestamp(1552884340.400742)
# 输出:datetime.datetime(2019, 3, 18, 12, 45, 40, 400742)
datetime.datetime.fromtimestamp(time.time())
# 输出:datetime.datetime(2019, 3, 18, 17, 10, 21, 564136)
datetime对象 → 时间戳
python
time.mktime(datetime.datetime.now().timetuple())
# 输出:1552900590.0
字符串 → datetime对象
python
datetime.datetime.strptime("2019-02-14 13:14:00", "%Y-%m-%d %H:%M:%S")
# 输出:datetime.datetime(2019, 2, 14, 13, 14)
datetime对象 → 字符串
python
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 输出:'2019-03-18 17:26:44'
时间元祖 → datetime对象
python
datetime.datetime.fromtimestamp(time.mktime(time.localtime()))
# 输出:datetime.datetime(2019, 3, 18, 17, 22, 13)
datetime对象 → 时间元祖
python
datetime.datetime.now().timetuple()
# 输出:time.struct_time(tm_year=2019, tm_mon=3, tm_mday=18, tm_hour=17, tm_min=22, tm_sec=53, tm_wday=0, tm_yday=77, tm_isdst=-1)
四、datetime,data,time对象转换
python
import time
from datetime import datetime
from datetime import date
from datetime import timedelta
datetime对象 → date对象
datetime.now().date()
# 输出:datetime.date(2019, 3, 17)
datetime对象 → time对象
datetime.now().time()
# 输出:datetime.time(17, 33, 3, 833546)
date对象 + time对象 → datetime对象
t1 = datetime.now().date()
t2 = datetime.now().time()
datetime.combine(t1, t2)
# 输出:datetime.datetime(2019, 3, 18, 17, 34, 7, 363993)
时间戳 → date对象
date.fromtimestamp(time.time())
# 输出:datetime.date(2019, 3, 17)