Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python Last Updated : 19 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to convert the DateTime string into the %Y-%m-%d-%H:%M:%S format. For this task strptime() and strftime() function is used. strptime() is used to convert the DateTime string to DateTime in the format of year-month-day hours minutes and seconds Syntax: datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") strftime() is used to convert DateTime into required timestamp format Syntax: datetime.strftime("%Y-%m-%d-%H:%M:%S") Here, %Y means year%m means month%d means day%H means hours%M means minutes%S means seconds. 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 Python3 # import datetime module from datetime import datetime # consider date in string format my_date = "30-May-2020-15:59:02" # convert datetime string into date,month,day and # hours:minutes:and seconds format using strptime d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") # convert datetime format into %Y-%m-%d-%H:%M:%S # format using strftime print(d.strftime("%Y-%m-%d-%H:%M:%S")) Output '2020-05-30-15:59:02' Example 2: Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format Python3 # import datetime module from datetime import datetime # consider date in string format my_date = "30-May-2020-15:59:02" # convert datetime string into date,month,day # and hours:minutes:and seconds format using # strptime d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") # convert datetime format into %Y-%m-%d-%H:%M:%S # format using strftime print(d.strftime("%Y-%m-%d-%H:%M:%S")) # consider date in string format my_date = "04-Jan-2021-02:45:12" # convert datetime string into date,month,day # and hours:minutes:and seconds format using # strptime d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") # convert datetime format into %Y-%m-%d-%H:%M:%S # format using strftime print(d.strftime("%Y-%m-%d-%H:%M:%S")) # consider date in string format my_date = "23-May-2020-15:59:02" # convert datetime string into date,month,day and # hours:minutes:and seconds format using strptime d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") # convert datetime format into %Y-%m-%d-%H:%M:%S # format using strftime print(d.strftime("%Y-%m-%d-%H:%M:%S")) Output2020-05-30-15:59:02 2021-01-04-02:45:12 2020-05-23-15:59:02 Comment More infoAdvertise with us Next Article Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python sravankumar_171fa07058 Follow Improve Article Tags : Python Python-datetime Practice Tags : python Similar Reads Pandas Convert Date (Datetime) To String Format Datetime is a data type in Pandas that provides a standardized way to represent and display dates and times in a human-readable format. It allows customization of output, such as displaying months, days, and years, offering flexibility in presenting temporal information. Necessity to convert Datetim 5 min read How to convert Python's .isoformat() string back into datetime object In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current tim 1 min read Convert string to datetime in Python with timezone Prerequisite: Datetime module In this article, we will learn how to get datetime object from time string using Python. To convert a time string into datetime object, datetime.strptime() function of datetime module is used. This function returns datetime object. Syntax : datetime.strptime(date_strin 3 min read Converting string into DateTime in Python The goal is to convert a date string like "2021/05/25" into a Python-recognized DateTime object such as 2021-05-25 00:00:00. This enables accurate and consistent date operations like comparisons, calculations and formatting when working with time-related data from sources like files or user input. L 2 min read How to change the Pandas datetime format in Python? Prerequisites: Pandas The date-time default format is "YYYY-MM-DD". Hence, December 8, 2020, in the date format will be presented as "2020-12-08". The datetime format can be changed and by changing we mean changing the sequence and style of the format. Function used strftime() can change the date 1 min read How to Convert String to Date or Datetime in Polars When working with data, particularly in CSV files or databases, it's common to find dates stored as strings. If we're using Polars, a fast and efficient DataFrame library written in Rust (with Python bindings), we'll often need to convert these strings into actual date or datetime objects for easier 5 min read Convert column type from string to datetime format in Pandas dataframe To perform time-series operations, dates should be in the correct format. Let's learn how to convert a Pandas DataFrame column of strings to datetime format. Pandas Convert Column To DateTime using pd.to_datetime()pd.to_datetime() function in Pandas is the most effective way to handle this conversio 4 min read How To Convert Date To Number String Or Text Format In Excel? Excel's built-in date system by default supports dates between January 1, 1900, and December 31, 9999. In this range, the numbers start at 1 and increase by 1 each time. In addition to text and numeric data types, Excel also allows dates that are internally stored as serial numbers. In Excel, the da 4 min read How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s 2 min read How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f 3 min read Like