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

Answer from Tenzin on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-datetime-string-to-yyyy-mm-dd-hhmmss-format-in-python
Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python - GeeksforGeeks
December 19, 2022 - First the take DateTime timestamp as a String. Then, convert it into DateTime using strptime(). Now, convert into the necessary format of DateTime using strftime · Example 1: Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format
🌐
Jingwen Zheng
jingwen-z.github.io › converting-between-datetime-and-string
Python: Converting between datetime and string - Jingwen Zheng
January 15, 2019 - >>> str_stamp = '2019-01-09' >>> datetime.datetime.strptime(str_stamp, '%Y-%m-%d') datetime.datetime(2019, 1, 9, 0, 0)
Discussions

python - Converting date between DD/MM/YYYY and YYYY-MM-DD? - Stack Overflow
Using a Python script, I need to read a CVS file where dates are formated as DD/MM/YYYY, and convert them to YYYY-MM-DD before saving this into a SQLite database. This almost works, but fails beca... More on stackoverflow.com
🌐 stackoverflow.com
pandas - Python how to convert datetime.date to the YYYY-MM-DD? - Stack Overflow
I have a two million timestamp data. I am trying to find first and last date that to in the YYYY-MM-DD format so I can use them in saving file name. But, I found out that np.unique(df.index) is fas... More on stackoverflow.com
🌐 stackoverflow.com
How can I convert YYYY-MM-DD to M/D/YYYY
I don't think that there's a library function to do that, unfortunately, so you're stuck manipulating it yourself. def to_m_d_yyyy(dt): return f"{dt.month}/{dt.day}/{dt.year}" In the REPL: >>> to_m_d_yyyy(datetime.datetime(2021, 8, 5)) '8/5/2021' More on reddit.com
🌐 r/learnpython
20
0
July 12, 2021
python - Convert a date string into YYYYMMDD - Stack Overflow
I've got a bunch of date strings in this form: - 30th November 2009 31st March 2010 30th September 2010 I want them like this: - YYYYMMDD Currently I'm doing this: - parsed_date = "30th Nove... More on stackoverflow.com
🌐 stackoverflow.com
August 4, 2011
People also ask

Are there third-party libraries for more advanced datetime operations in Python?
Yes, there are several third-party libraries, such as arrow, pendulum, and dateparser, which offer advanced features for datetime manipulation, time zone handling, and more. These libraries can simplify complex datetime operations in Python.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to datetime python
Convert String to Datetime Python: Comprehensive Guide & Examples
What are the common mistakes when working with datetime objects in Python?
Common mistakes include neglecting time zone considerations, not handling daylight saving time transitions, and overlooking the importance of formatting and parsing datetime strings accurately.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to datetime python
Convert String to Datetime Python: Comprehensive Guide & Examples
Are there any limitations to handling extremely distant past or future dates with Python datetime objects?
Python's datetime module has limitations when working with dates outside a certain range, typically around 1 AD to 9999 AD. Handling dates beyond this range may require custom implementations or alternative libraries.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to datetime python
Convert String to Datetime Python: Comprehensive Guide & Examples
Top answer
1 of 4
39

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

2 of 4
23

converting string 'yyyy-mm-dd' into 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()

the 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 doesn't take into account holidays.

🌐
GeeksforGeeks
geeksforgeeks.org › python › converting-string-yyyy-mm-dd-into-datetime-in-python
Converting string into DateTime in Python - GeeksforGeeks
July 23, 2025 - datetime.strptime() method, part of Python's datetime module, efficiently converts a date string into a DateTime object when the exact format is known, requiring a format specification like '%Y/%m/%d'.
Find elsewhere
🌐
Java2Blog
java2blog.com › home › python › python date › format date to yyyymmdd in python
Format Date to YYYYMMDD in Python - Java2Blog
November 27, 2023 - Formatting dates into specific string representations is a crucial task in Python, especially in areas like data processing and reporting. One common format is “YYYYMMDD,” which is often used for its simplicity and ease of sorting. For instance, given a Python datetime object representing a date, say 2021-11-27, and we need to format this date into YYYYMMDD format, resulting in 20211127.
🌐
Reddit
reddit.com › r/learnpython › how can i convert yyyy-mm-dd to m/d/yyyy
r/learnpython on Reddit: How can I convert YYYY-MM-DD to M/D/YYYY
July 12, 2021 -

Hello all,

I have an excel file with about 100 columns, and 30 or so are dates, I would like to convert all the date formats

from:

 YYYY-MM-DD 

to

  M/D/YYYY

I was able to change it to MM/DD/YYYY using the following code

def fmt(input_dt):
if isnull(input_dt):
    return ""
else:
    return input_dt.strftime("%m/%d/%Y")

for col in df.columns:
if df[col].dtype == 'datetime64[ns]':
    df[col] = df[col].apply(fmt)

but that gives me

MM/DD/YYYY

I also need it to be datetime when exported back to excel.

I looked into the documentation

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

But it does not have M/D/YYYY any suggestions would be helpful. Thank you! Also if there is a more pythonic way to write it please let me know

🌐
Upgrad
upgrad.com › home › tutorials › software & tech › convert string to datetime python
Convert String to Datetime Python: Comprehensive Guide & Examples
November 13, 2024 - from datetime import datetime date_str = "2023-10-14 15:30:00" format_str = "%Y-%m-%d %H:%M:%S" date_obj = datetime.strptime(date_str, format_str) print(date_obj) Output of Python string to datetime yyyy-mm-dd: 2023-10-14 15:30:00
🌐
TutorialsPoint
tutorialspoint.com › How-to-convert-Python-date-string-mm-dd-yyyy-to-datetime
How to convert Python date string mm/dd/yyyy to datetime?
September 28, 2023 - In Python, you can convert a string to date object using the strptime() function. Provide the date string and the format in which the date is specified. Example import datetime date_str = '29/12/2017' # The date - 29 Dec 2017 format_str = '%d/%m/%Y
🌐
iO Flood
ioflood.com › blog › python-datetime-to-string
Learn Python: Convert datetime to string (With Examples)
February 7, 2024 - Two of these methods are isoformat() and __str__(). The isoformat() method returns a string representing the date in ISO 8601 format, which is ‘YYYY-MM-DDTHH:MM:SS’. Let’s see it in action:
🌐
Esri Community
community.esri.com › t5 › python-questions › convert-date-to-yyyymmdd-string-in-field › td-p › 616266
Convert Date to yyyymmdd string in field calculator with Python
March 14, 2022 - This is for people who want to do what I specifically needed: To calculate a date field from a string field formatted YYYYMMDD. Hope this adds clarity and helps someone! ... Be aware that cursors and field calculator return actual datetime objects in ArcGIS Pro.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to format dates in python to yyyy-mm-dd
5 Best Ways to Format Dates in Python to YYYY-MM-DD - Be on the Right Side of Change
February 27, 2024 - One of the most common and straightforward methods to format dates in Python is using the datetime.strftime() function from the datetime module. This method enables you to represent a datetime object as a string in the “YYYY-MM-DD” format ...
🌐
Squash
squash.io › how-to-get-todays-date-in-yyyy-mm-dd-format-in-python
How to Get Today's Date in YYYY MM DD Format in Python
November 2, 2023 - Related Article: Tutorial on Python Generators and the Yield Keyword · Another approach is to use the strptime() method to parse the current date as a string and then use the strftime() method to format it.
🌐
PYnative
pynative.com › home › python › python datetime › python datetime format using strftime()
Python DateTime Format using Strftime() – PYnative
May 6, 2022 - Use datetime.strftime(format) to convert a datetime object into a string as per the corresponding format. The format codes are standard directives for mentioning in which format you want to represent datetime.
🌐
Tutorial Teacher
tutorialsteacher.com › articles › convert-string-to-datetime-in-python
Convert String to Datetime in Python
>>> strdate="2020-10-16 12:35:20" >>> datetimeobj=datetime.datetime.strptime(strdate, "%Y-%m-%d %H:%M:%S") >>> datetimeobj datetime.datetime(2020, 10, 16, 12, 35, 20) If the date string is changed to 'DD-MM-YY', the format has to be set to %d-%m-%Y.
🌐
Python
docs.python.org › 3.4 › library › datetime.html
8.1. datetime — Basic date and time types — Python 3.4.10 documentation
Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For example, date(2002, 12, 4).isoformat() == '2002-12-04'.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-datetime-strptime
How To Convert a String to a datetime Object in Python | DigitalOcean
December 13, 2024 - First, parse the string into a datetime object and then format it into the desired yyyy-mm-dd string format: from datetime import datetime date_string = "12 25 2024" date_object = datetime.strptime(date_string, "%m %d %Y") formatted_date = date_object.strftime("%Y-%m-%d") print(formatted_date)
🌐
PythonHow
pythonhow.com › how › convert-a-string-into-datetime-format
Here is how to convert a string into datetime format in Python
To convert a string into a datetime object in Python, you can use the strptime() method from the datetime module. This method takes the string to be converted and the format in which the string represents a date and time, and it returns a datetime object that represents the same date and time.