How to convert string in a datetime.datetime format into a datetime object?

Jeff Pang

I have a raw log which print timestamp in format of datetime.datetime(2021, 9, 10, 10, 15, 0, 250362). I believe some conversion has failed in the underlying program (out of my control).

Since it's a string, I can't do it with strftime().

Any way I could translate this into a datetime?

Mark Tolonen

datetime.strptime() can be used to validate a string and parse a date/time:

from datetime import datetime

s = 'datetime.datetime(2021, 9, 10, 10, 15, 0, 250362)'
dt = datetime.strptime(s,'datetime.datetime(%Y, %m, %d, %H, %M, %S, %f)')
print(dt)

Output:

2021-09-10 10:15:00.250362

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert string to datetime format in pandas python?

how to convert a string date into datetime format in python?

how to convert a string date into datetime format in python?

convert datetime string to datetime object in dart?

How to convert datetime from string format into datetime format in pyspark?

How to convert string to datetime?

How to convert string data with variable format to datetime?

How to convert DateTime to a String

Convert string of datetime to datetime object

Convert string to DateTime and format

DateTime from PHP to Java (Android): how to convert a string to a DateTime object?

How to convert a string to a datetime format in MySQL

How to convert string to datetime(datetime) in same format?

Android how to convert string into datetime format?

How to convert datetime string to another format

AngularJs Convert string datetime to datetime object

Convert datetime string in datetime format in SQL server

How to convert a string to datetime object

How to convert this UTC datetime string to DateTime object c#

How do I convert string into datetime.datetime object?

How convert DateTime to string

Convert date string format to a datetime Python Object

Convert iso format datetime string to datetime object in Python

How to format string to be a default datetime object

Convert string in format datetime

How to convert string timeseries to datetime object in a dataframe while preserving the format of the original datetime?

How to Convert DateTime String to UTC format in Kotlin

how to convert string to dictionary with a datetime.datetime object in it

Python: How to convert date string into datetime object