user3800040
user3800040

Reputation: 71

Month starting date and Ending date

How to get Starting Date and Ending Date, ex my Month has like this jan =1 year = 2013 feb =2 year =2013 mar =3 year = 2013 like this, i want like this if i select 1 means 1-1-2013 to 31-1-2013, how to get this.

Upvotes: 1

Views: 154

Answers (2)

xecgr
xecgr

Reputation: 5193

Standard solution, without dependences

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

Once you get datetime object, you can print it with datetime.strftime function

Upvotes: 4

Jakob Bowyer
Jakob Bowyer

Reputation: 34688

This is trivial in arrow (you will have to install arrow using pip install arrow)

import arrow
a = arrow.get(month=5)
print a.ceil("month")
print a.floor("month")

Upvotes: 0

Related Questions