Convert string 'yyyy-mm-dd' to datetime python

I have the original input from a user, such as "2015-01-30" ... for the query I'm using, the date should be entered as a string as such, "yyyy-mm-dd".

I would like to increase the date by 1 month at the end of my cycle st "2015-01-30" will become "2015-02-27" (ideally, the last working day of the next month). I was hoping someone could help me; I use PYTHON, the reason I want to convert to datetime, I found a function that adds 1 month.

Ideally, my two questions to answer are (in Python):

1) how to convert the string "yyyy-mm-dd" to datetime python and convert back to a string after applying the timedelta function p>

2) And / or how to add 1 month to the string "yyyy-mm-dd"

+8
python datetime timedelta
source share
4 answers

You can use a single line that takes datetime , adds a month (using a specific function) and converts back to a string:

 x = add_months(datetime.datetime(*[int(item) for item in x.split('-')]), 1).strftime("%Y-%m-%d") 

 >>> import datetime, calendar >>> x = "2015-01-30" >>> x = add_months(datetime.datetime(*[int(item) for item in x.split('-')]), 1).strftime("%Y-%m-%d") >>> x '2015-02-28' >>> 

add_months :

 def add_months(sourcedate,months): month = sourcedate.month - 1 + months year = sourcedate.year + month / 12 month = month % 12 + 1 day = min(sourcedate.day,calendar.monthrange(year,month)[1]) return datetime.date(year,month,day) 
+2
source share

Perhaps these examples will help you understand:

 from dateutil.relativedelta import relativedelta import datetime date1 = datetime.datetime.strptime("2015-01-30", "%Y-%m-%d").strftime("%d-%m-%Y") print(date1) today = datetime.date.today() print(today) addMonths = relativedelta(months=3) future = today + addMonths print(future) 

If you import the date and time, this will give you more control over the date and time variables.
In my example above, I have sample code that will show you how it works.

Also very useful if you would like to add x the number of days, months or years before a specific date.

Edit: To answer the question below this post, I would suggest you look at the "calendar"

For example:

 import calendar january2012 = calendar.monthrange(2002,1) print(january2012) february2008 = calendar.monthrange(2008,2) print(february2008) 

This returns you the first business day of the month and the number of days of the month.
With this, you can calculate what was the last business day of the month.
Here is more detailed information: Link
There is also a link here, it looks like you could use: Link

+10
source share

convert string 'yyyy-mm-dd' to datetime / date python

 from datetime import date date_string = '2015-01-30' now = date(*map(int, date_string.split('-'))) # or now = datetime.strptime(date_string, '%Y-%m-%d').date() 

last business day of the next month

 from datetime import timedelta DAY = timedelta(1) last_bday = (now.replace(day=1) + 2*31*DAY).replace(day=1) - DAY while last_bday.weekday() > 4: # Sat, Sun last_bday -= DAY print(last_bday) # -> 2015-02-27 

It does not take into account the holidays.

+6
source share

To convert a string of this format to a Python date object:

 In [1]: import datetime In [2]: t = "2015-01-30" In [3]: d = datetime.date(*(int(s) for s in t.split('-'))) In [4]: d Out[4]: datetime.date(2015, 1, 30) 

To go to the last day of the next month:

 In [4]: d Out[4]: datetime.date(2015, 1, 30) In [5]: new_month = (d.month + 1) if d.month != 12 else 1 In [6]: new_year = d.year if d.month != 12 else d.year + 1 In [7]: import calendar In [8]: new_day = calendar.monthrange(new_year, new_month)[1] In [9]: d = d.replace(year=new_year,month=new_month,day=new_day) In [10]: d Out[10]: datetime.date(2015, 2, 28) 

And this datetime.date object can be easily converted to the string "YYYY-MM-DD":

 In [11]: str(d) Out[11]: '2015-02-28' 

EDIT:

To get the last business day (i.e. Monday through Friday) of the month:

 In [8]: new_day = calendar.monthrange(new_year, new_month)[1] In [9]: d = d.replace(year=new_year,month=new_month,day=new_day) In [10]: day_of_the_week = d.isoweekday() In [11]: if day_of_the_week > 5: ....: adj_new_day = new_day - (day_of_the_week - 5) ....: d = d.replace(day=adj_new_day) ....: In [11]: d Out[11]: datetime.date(2015, 2, 27) 
0
source share

All Articles