Python 月份开始日期和结束日期

Python 月份开始日期和结束日期,python,Python,如何获取开始日期和结束日期,例如我的月份有这样的一月=1year=2013feb=2year=2013mar=3year=2013像这样,如果我选择1意味着2013年1月1日到2013年1月31日,我想要这样的,如何获取此信息。这在arrow中很简单(您必须使用pip安装arrow安装arrow) 这在arrow中很简单(您必须使用pip安装arrow安装arrow) 标准溶液,无依赖性 >>> from datetime import datetime,timedelta &

如何获取开始日期和结束日期,例如我的月份有这样的一月=
1
year=2013
feb=
2
year=2013
mar=
3
year=2013
像这样,如果我选择1意味着2013年1月1日到2013年1月31日,我想要这样的
,如何获取此信息。

这在
arrow
中很简单(您必须使用
pip安装arrow安装arrow


这在
arrow
中很简单(您必须使用
pip安装arrow安装arrow


标准溶液,无依赖性

>>> from datetime import datetime,timedelta
>>> month = 1 #jan
>>> day = 1
>>> year = datetime.now().year #if you want current year, year's value otherwise 
>>> dt = datetime(year,month,day)
datetime.datetime(2014, 1, 1, 0, 0)
>>> if month==12:
...     year += 1
...     month = 0
>>> dt_end_month = datetime(year,month+1,day) - timedelta(days=1)
>>> dt_end_month
datetime.datetime(2014, 1, 31, 0, 0)

一旦获得datetime对象,就可以使用标准解决方案打印它,而无需依赖项

>>> from datetime import datetime,timedelta
>>> month = 1 #jan
>>> day = 1
>>> year = datetime.now().year #if you want current year, year's value otherwise 
>>> dt = datetime(year,month,day)
datetime.datetime(2014, 1, 1, 0, 0)
>>> if month==12:
...     year += 1
...     month = 0
>>> dt_end_month = datetime(year,month+1,day) - timedelta(days=1)
>>> dt_end_month
datetime.datetime(2014, 1, 31, 0, 0)
一旦获得datetime对象,就可以使用
datetime.strftime
函数打印它