繁体   English   中英

如何从 python 中的日期获取月份名称

[英]How to get month name from date in python

目标是从上个月的日期获取月份名称和年份。 如: October -2019

但我得到一个错误:

import datetime


d = datetime.date.today()
first_day = d.replace(day=1) # gives first day of current month
last_day = first_day - datetime.timedelta(days=1) # gives last date of previous month
print(last_day.strtime("%B"))

错误

Traceback (most recent call last):
  File "x:\Documents\Python\tempCodeRunnerFile.py", line 7, in <module>
    print(last_day.strtime("%B"))
AttributeError: 'datetime.date' object has no attribute 'strtime'

使用的正确方法称为strftime ,而不是strtime

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM