import calendar
print(calendar.isleap(2000))
out
True
print(calendar.weekheader(4))
out
Mon Tue Wed Thu Fri Sat Sun
print(calendar.weekday(2023, 9, 11)) # index, 0~6
print(calendar.day_name[calendar.weekday(2023, 9, 11)])
out
0
Monday
print(calendar.prmonth(2023, 9))
output
September 2023
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
None
今天星期几,对应英文
import calendar
print(calendar.weekday(2022, 3, 24)) # 0~6
3
Thursday
# weekday
import datetime
week_list = ["星期一","星期二","星期三","星期四","星期五","星期六","星期七"]
print(week_list[datetime.date(2022, 3, 24).weekday()]) # 返回 0~6
星期四
import datetime
print(datetime.date(2022,3,24).isoweekday()) # 返回 1~7
4
import datetime
print(datetime.date(2022,3,24).strftime("%A"))
print(datetime.date(2022,3,24).strftime("%a"))
Thursday
Thu
import pandas as pd
from datetime import datetime
df = pd.DataFrame({
"name": ["张三", "李四", "王五"],
"date": [datetime(2022,3,22), datetime(2022,3,23), datetime(2022,3,24)]
})
print(df,"\n")
df["week_num1"] = df["date"].dt.dayofweek
df["week_num2"] = df["date"].dt.weekday
df["week_name"] = df["date"].dt.day_name()
print(df)
output
name date
0 张三 2022-03-22
1 李四 2022-03-23
2 王五 2022-03-24
name date week_num1 week_num2 week_name
0 张三 2022-03-22 1 1 Tuesday
1 李四 2022-03-23 2 2 Wednesday
2 王五 2022-03-24 3 3 Thursday
肝了3天,整理了80个Python DateTime 例子,必须收藏!
import time
t = time.localtime()
print (time.asctime(t))
output
Tue May 24 11:50:38 2022
gmtime 和 strftime
from time import gmtime,strftime
print(strftime("%a, %d %b %Y %H:%M:%S", gmtime()))
print(strftime("%A", gmtime())) # 星期
print(strftime("%D", gmtime())) # 日期
print(strftime("%B", gmtime())) # 月份
print(strftime("%y", gmtime())) # 年份
# Convert seconds into GMT date
print(strftime("%a, %d %b %Y %H:%M:%S", gmtime(1)))
print(strftime("%a, %d %b %Y %H:%M:%S", gmtime(123456789)))
Tue, 24 May 2022 11:50:38
Tuesday
05/24/22
May
22
Thu, 01 Jan 1970 00:00:01
Thu, 29 Nov 1973 21:33:09
import pandas as pd
print(pd.datetime.now())
print(pd.datetime.now().date())
print(pd.datetime.now().year)
print(pd.datetime.now().month)
print(pd.datetime.now().day)
print(pd.datetime.now().hour)
print(pd.datetime.now().minute)
print(pd.datetime.now().second)
print(pd.datetime.now().microsecond)
output
2022-05-24 11:52:59.556948
2022-05-24
2022
5
24
11
52
59
557247
import os
import time
pth = "D://model/"
for roots, dirs, files in os.walk(pth):
for file in files:
obsolute_path = os.path.join(roots, file)
mtime = os.stat(obsolute_path).st_mtime
file_modify_time = time.strftime('%Y-%m-%d,%H:%M:%S', time.localtime(mtime))
print("{i} 修改时间是 {j}".format(i=obsolute_path, j=file_modify_time))
STXINGKA.TTF
是华文行楷
参考 用Python自动化生成倒计时图片
from PIL import Image, ImageDraw, ImageFont
import os
for i in range(1, 22):
# 创建图像,设置图像大小及颜色
im = Image.new('RGBA', (1000, 1800), (166, 12, 4, 255))
draw = ImageDraw.Draw(im)
# 设置本次使用的字体
fontsFolder = 'C:\'Windows\Fonts'
font1 = ImageFont.truetype(os.path.join(fontsFolder, 'STXINGKA.TTF'), 420)
font2 = ImageFont.truetype(os.path.join(fontsFolder, 'STXINGKA.TTF'), 40)
# 计算各文本的放置位置
txtSize_1 = draw.textsize('距 离 除 夕 夜', font2)
pos_x_1 = (1000 - txtSize_1[0]) / 2
txtSize_2 = draw.textsize('还 有', font2)
pos_x_2 = (1000 - txtSize_2[0]) / 2
txtSize_3 = draw.textsize('天', font2)
pos_x_3 = (1000 - txtSize_3[0]) / 2
txtSize_4 = draw.textsize('不 是 年 味 越 来 越 少', font2)
pos_x_4 = (1000 - txtSize_4[0]) / 2
txtSize_5 = draw.textsize('而 是 我 们 都 长 大 了', font2)
pos_x_5 = (1000 - txtSize_5[0]) / 2
# 设置文本放置位置,居中
draw.text((pos_x_1, 200), '距 离 除 夕 夜', fill=(217, 217, 217, 255), font=font2)
draw.text((pos_x_2, 300), '还 有', fill=(217, 217, 217, 255), font=font2)
draw.text((pos_x_3, 1050), '天', fill=(217, 217, 217, 255), font=font2)
draw.text((pos_x_4, 1350), '不 是 年 味 越 来 越 少', fill=(137, 183, 109, 255), font=font2)
draw.text((pos_x_5, 1440), '而 是 我 们 都 长 大 了', fill=(137, 183, 109, 255), font=font2)
# 绘制线框
draw.line([(20, 20), (980, 20), (980, 1780), (20, 1780), (20, 20)], fill=(217, 217, 217, 255), width=5)
# 设置变化的文本属性
txtSize_6 = draw.textsize(str(i), font1)
pos_x_6 = (1000 - txtSize_6[0]) / 2
draw.text((pos_x_6, 500), str(i), fill=(137, 183, 109, 255), font=font1)
# im.show()
# 保存图像
filename = 'day' + str(i) + '.png'
im.save(filename)
生成了 1-21 天,部分节选如下