所需模块为datetime模块,该模块是Python的内置模块,无需利用pip再安装。该模块可以显示日期和时间,还可进行日期和时间的运算及格式化。
返回值 | 对应星期 |
---|---|
0 | 星期一 |
1 | 星期二 |
2 | 星期三 |
3 | 星期四 |
4 | 星期五 |
5 | 星期六 |
6 | 星期日 |
import datetime
week = datetime.date(2022, 11, 13).weekday()
print(week) # 6 对应的是星期日
import datetime
week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
week_day = week[datetime.date(2022, 11, 13).weekday()]
print(week_day) # 星期日
返回值 | 对应星期 |
---|---|
1 | 星期一 |
2 | 星期二 |
3 | 星期三 |
4 | 星期四 |
5 | 星期五 |
6 | 星期六 |
7 | 星期日 |
import datetime
print(datetime.date(2022, 11, 13).isoweekday()) # 7
import datetime
print(datetime.date(2022, 11, 13).strftime("%A")) # Sunday
import datetime
print(datetime.date(2022, 11, 13).strftime("%a")) # Sun
calendar日历模块也是内置库,提供了很多处理日历的方法,可用其制作日历、周历等。可用weekday()和day_name()方法查询某日期是星期几。
import calendar
print(calendar.weekday(2022, 11, 13)) # 6
import calendar
date_week = calendar.weekday(2022, 11, 13)
print(calendar.day_name[date_week]) # Sunday
返回值 | 对应星期 |
---|---|
0 | 星期日 |
1 | 星期一 |
2 | 星期二 |
3 | 星期三 |
4 | 星期四 |
5 | 星期五 |
6 | 星期六 |
import pendulum # pip install pendulum
print(pendulum.parse('2022-11-13').day_of_week) # 0 对应星期天
import pandas as pd
from datetime import datetime
df = pd.DataFrame({
"name": ["张三", "李四", "朱五"],
"date": [datetime(2022, 11, 13), datetime(2022, 11, 14), datetime(2022, 11, 15)]
})
print(df)
print()
df['week_num1'] = df['date'].dt.dayofweek
df['week_num2'] = df['date'].dt.weekday
df['week_name'] = df['date'].dt.day_name()
print(df)
运行结果:
name date
0 张三 2022-11-13
1 李四 2022-11-14
2 朱五 2022-11-15
name date week_num1 week_num2 week_name
0 张三 2022-11-13 6 6 Sunday
1 李四 2022-11-14 0 0 Monday
2 朱五 2022-11-15 1 1 Tuesday
Process finished with exit code 0
import datetime
date1 = datetime.date(2022, 11, 23)
date2 = datetime.date(2022, 11, 30)
print(date1.__lt__(date2)) # True
import datetime
date1 = datetime.date(2022, 11, 23)
date2 = datetime.date(2022, 11, 30)
print(date1.__gt__(date2)) # False
import datetime
# 以下两种标准格式都可以
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time3 = "2019/1/2 11:00:00"
time4 = datetime.datetime.strptime(time3, "%Y/%m/%d %H:%M:%S")
print(type(time2)) #
print(type(time4)) #
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 += datetime.timedelta(days=1) # 增加天数
print(time2) # 2019-01-03 11:00:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 -= datetime.timedelta(days=5) # 减少天数
print(time2) # 2018-12-28 11:00:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 += datetime.timedelta(hours=5) # 增加小时
print(time2) # 2019-01-02 16:00:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 -= datetime.timedelta(hours=5) # 减少小时
print(time2) # 2019-01-02 06:00:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 += datetime.timedelta(minutes=20) # 增加分钟
print(time2) # 2019-01-02 11:20:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 -= datetime.timedelta(minutes=20) # 减少分钟
print(time2) # 2019-01-02 10:40:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 += datetime.timedelta(seconds=20) # 增加秒数
print(time2) # 2019-01-02 11:00:20
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 -= datetime.timedelta(seconds=20) # 减少秒数
print(time2) # 2019-01-02 10:59:40
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 += datetime.timedelta(days=368) # 增加年份
print(time2) # 2020-01-05 11:00:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 -= datetime.timedelta(days=368) # 减少年份
print(time2) # 2017-12-30 11:00:00
import datetime
time1 = "2019-1-2 11:00:00"
time2 = datetime.datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
time2 -= datetime.timedelta(days=1, hours=1, minutes=20, seconds=30) # 减少年份
print(time2) # 2019-01-01 09:39:30
参考文章:
https://blog.csdn.net/cainiao_python/article/details/123516004
https://jingyan.baidu.com/article/e5c39bf58f809378d6603337.html
https://zhuanlan.zhihu.com/p/461500036