PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2023-04-15 12:40:25  修改时间:2024-11-20 20:53:39  分类:10.Python基础  编辑

一、日期月份转为英文(简写)

import calendar

date_list = [1,2,3,4,5,6,7,8,9,10,11,12]

#数字转为月份简写
for i in date_list:
    print(calendar.month_abbr[i])

输出:

Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

二、日期月份转为英文(全称)

date_list = [1,2,3,4,5,6,7,8,9,10,11,12]

#数字转月份的全写
for i in date_list:
    print(calendar.month_name[i])

输出:

January
February
March
April
May
June
July
August
September
October
November
December

三、日期月份英文转为数字月份

# 简写月份转数字

month_abbr_list = ['Jan',
 'Feb',
 'Mar',
 'Apr',
 'May',
 'Jun',
 'Jul',
 'Aug',
 'Sep',
 'Oct',
 'Nov',
 'Dec']

for i in month_abbr_list:
    print( list(calendar.month_abbr).index(i) )

输出:

1
2
3
4
5
6
7
8
9
10
11
12

 

 

参考:

  1. Python将日期月份转为英文和英文转为数字月份
  2. Python 英文的月份转数字及数字转英文