Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string 'yyyy-mm-dd' into datetime [duplicate]

I have a raw input from the user such as "2015-01-30"...for the query I am using, the date has to be inputed as a string as such "yyyy-mm-dd".

I would like to increment the date by 1 month at end of my loop s.t "2015-01-30" becomes "2015-02-27" (ideally the last business day of the next month). I was hoping someone could help me; I am using PYTHON, the reason I want to convert to datetime is I found a function to add 1 month.

Ideally my two questions to be answered are (in Python):

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

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

like image 932
Udaya Tenneti Avatar asked Apr 21 '15 17:04

Udaya Tenneti


People also ask

How do you convert MM DD to YYYY datetime?

yyyy-mm-dd stands for year-month-day . We can convert string format to datetime by using the strptime() function. We will use the '%Y/%m/%d' format to get the string to datetime.

How do I change date format from YYYY MM DD in Python?

We can use strftime() to format dates and change the date format easily of a date or datetime object. For example, if you want to change the date format to MM/DD/YYYY, you can pass “%m/%d/%Y” to strftime(). Another useful code for formatting dates is the month name code “%B”.

How do I convert a string to a datetime in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


1 Answers

Maybe these examples will help you get an idea:

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 datetime it will give you more options in managing date and time variables.
In my example above I have some example code that will show you how it works.

It is also very usefull if you would for example would like to add a x number of days, months or years to a certain date.

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

For example:

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

This return you the first workday of the month, and the number of days of the month.
With that you can calculate what was the last workday of the month.
Here is more information about it: Link
Also have a loook here, looks what you might could use: Link

like image 188
Tenzin Avatar answered Oct 13 '22 11:10

Tenzin