从数字中获取月份名称

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.

  • `mydate.strftime("%b")`返回月份名称的简短表示法.(对于上面的例子,它将返回`Dec`) (10认同)
  • OP想要使用整数作为输入,而不是 datetime.datetime 对象。您可以使用 mydate = datetime.datetime(2019, integer , 1, 0, 0) 但它相当丑陋 (6认同)
  • 如果您只需知道给定数字(1 - 12)的月份名称,那么这不是很有用,因为当前日期并不重要.calendar.month_name [i]或calendar.month_abbr [i]在这里更有用. (5认同)
  • 请注意,如果使用非英语语言环境,%B 可能会返回大小写错误的月份名称。例如,对于 ru_RU 语言环境,%B 返回“января”而不是“январь”。 (3认同)
  • 我不明白这如何回答这个问题。 (2认同)

sia*_*ame 237

Calendar API

从那里你可以看到calendar.month_name[3]它将返回March,并且数组索引0是空字符串,所以也不必担心零索引.

  • 您还可以使用calendar.month_abbr [i]作为短月份名称 - 例如3月. (30认同)
  • 注意:您可能需要在文件顶部“导入日历”才能使用它。 (9认同)
  • 如果有人想在 pandas 中执行此操作,解决方案如下:/sf/ask/2520769961/。只是这是在字符串格式问题中查找月份名称时出现的第一个结果。想放在这里以防万一。 (2认同)

小智 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 文档

\n

所有 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)


The*_*Pig 8

这就是我要做的事:

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)

(这是我写这篇文章的真实日期)


cgl*_*cet 8

一些好的 答案已经使用了日历,但尚未提及设置语言环境的效果。

日历根据当前语言环境设置月份名称,例如法语:

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() […]

  • 很好的补充,只是我建议添加后备区域性或仅将 setLocale 调用包装在 try/ except 中,因为如果该区域性不可用,您会得到一个异常(尝试此操作后就发生在我身上;)) (2认同)

小智 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)