首页 > 代码库 > python脚本之日期格式显示

python脚本之日期格式显示

脚本内容:

#!/usr/bin/python
#coding=utf-8
#根据给定的年月日以数字形式打印出日期
months =[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
#以1-31的数字作为结尾的列表
ending =["st","nd",‘rd‘]+17 * [‘th‘] \
+["st","nd","rd"]+7 * [‘th‘] \
+[‘st‘]
year =raw_input("year: ")
month =raw_input(‘month(1-12): ‘)
day =raw_input(‘day(1-31): ‘)
month_number =int(month)
day_number =int(day)
#记得要将月份和天数减1,以获得正确的索引
ordinal = day + ending[day_number-1]
month_name = months[month_number-1]
print month_name + ‘ ‘ +ordinal +‘,‘+year

 

效果:分别输入 年,月,日,得到我们想要的格式

技术分享

 

python脚本之日期格式显示