Raj*_*eev 202 python datetime date
如何从月份编号中获取月份名称?
例如,如果我有3
,我想回来march
date.tm_month()
Run Code Online (Sandbox Code Playgroud)
如何获得字符串march
?
JMa*_*Max 334
import datetime
mydate = datetime.datetime.now()
mydate.strftime("%B")
Run Code Online (Sandbox Code Playgroud)
回报:12月
有关Python doc网站的更多信息
[编辑:来自@GiriB的好评]你也可以使用%b
它返回月份名称的短符号.
mydate.strftime("%b")
Run Code Online (Sandbox Code Playgroud)
对于上面的例子,它将返回Dec
.
sia*_*ame 237
从那里你可以看到calendar.month_name[3]
它将返回March
,并且数组索引0
是空字符串,所以也不必担心零索引.
小智 48
import datetime
monthinteger = 4
month = datetime.date(1900, monthinteger, 1).strftime('%B')
print month
Run Code Online (Sandbox Code Playgroud)
四月
Jay*_*eth 27
如果您只需知道给定数字(1 - 12)的月份名称,那么这不是很有用,因为当前日期并不重要.
calendar.month_name[i]
要么
calendar.month_abbr[i]
在这里更有用.
这是一个例子:
import calendar
for month_idx in range(1, 13):
print (calendar.month_name[month_idx])
print (calendar.month_abbr[month_idx])
print ("")
Run Code Online (Sandbox Code Playgroud)
样本输出:
January
Jan
February
Feb
March
Mar
...
Run Code Online (Sandbox Code Playgroud)
anc*_*cho 18
import datetime
mydate = datetime.datetime.now()
mydate.strftime("%B") # 'December'
mydate.strftime("%b") # 'dec'
Run Code Online (Sandbox Code Playgroud)
小智 14
datetime
\xe2\x80\x94 基本日期和时间类型 \xe2\x80\x94 Python 文档
所有 strftime 格式代码的列表。月份名称和格式化等好东西都用零填充。阅读整页内容,了解“朴素”论证的规则等内容。以下是简要列表:
\n%a Sun, Mon, \xe2\x80\xa6, Sat \n%A Sunday, Monday, \xe2\x80\xa6, Saturday \n%w Weekday as number, where 0 is Sunday \n%d Day of the month 01, 02, \xe2\x80\xa6, 31\n%b Jan, Feb, \xe2\x80\xa6, Dec\n%B January, February, \xe2\x80\xa6, December\n%m Month number as a zero-padded 01, 02, \xe2\x80\xa6, 12\n%y 2 digit year zero-padded 00, 01, \xe2\x80\xa6, 99\n%Y 4 digit Year 1970, 1988, 2001, 2013\n%H Hour (24-hour clock) zero-padded 00, 01, \xe2\x80\xa6, 23\n%I Hour (12-hour clock) zero-padded 01, 02, \xe2\x80\xa6, 12\n%p AM or PM.\n%M Minute zero-padded 00, 01, \xe2\x80\xa6, 59\n%S Second zero-padded 00, 01, \xe2\x80\xa6, 59\n%f Microsecond zero-padded 000000, 000001, \xe2\x80\xa6, 999999\n%z UTC offset in the form +HHMM or -HHMM +0000, -0400, +1030\n%Z Time zone name UTC, EST, CST\n%j Day of the year zero-padded 001, 002, \xe2\x80\xa6, 366\n%U Week number of the year zero padded, Days before the first Sunday are week 0\n%W Week number of the year (Monday as first day)\n%c Locale\xe2\x80\x99s date and time representation. Tue Aug 16 21:30:00 1988\n%x Locale\xe2\x80\x99s date representation. 08/16/1988 (en_US)\n%X Locale\xe2\x80\x99s time representation. 21:30:00 \n%% literal \'%\' character.\n
Run Code Online (Sandbox Code Playgroud)\n
小智 9
一次打印所有月份:
import datetime
monthint = list(range(1,13))
for X in monthint:
month = datetime.date(1900, X , 1).strftime('%B')
print(month)
Run Code Online (Sandbox Code Playgroud)
这就是我要做的事:
from datetime import *
months = ["Unknown",
"January",
"Febuary",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"]
now = (datetime.now())
year = (now.year)
month = (months[now.month])
print(month)
Run Code Online (Sandbox Code Playgroud)
它输出:
>>> September
Run Code Online (Sandbox Code Playgroud)
(这是我写这篇文章的真实日期)
一些好的 答案已经使用了日历,但尚未提及设置语言环境的效果。
日历根据当前语言环境设置月份名称,例如法语:
import locale
import calendar
locale.setlocale(locale.LC_ALL, 'fr_FR')
assert calendar.month_name[1] == 'janvier'
assert calendar.month_abbr[1] == 'jan'
Run Code Online (Sandbox Code Playgroud)
如果您打算在setlocale
代码中使用,请务必阅读文档中的提示和警告以及扩展编写器部分。此处显示的示例并不代表应如何使用它。特别是,从这两个部分:
在某些库例程中调用 setlocale() 通常是一个坏主意,因为作为副作用,它会影响整个程序 [...]
扩展模块永远不应该调用 setlocale() […]
小智 6
我会提供这个以防万一(就像我)你在数据帧中有一列月份数:
df['monthName'] = df['monthNumer'].apply(lambda x: calendar.month_name[x])
Run Code Online (Sandbox Code Playgroud)
小智 6
“01”到“一月”
from datetime import datetime
datetime.strptime('01', "%m").strftime("%b")
Run Code Online (Sandbox Code Playgroud)