Created
December 6, 2022 05:58
-
-
Save chandra-prakash-meghwal/7a5fc9122b2542aa5806f7dcb7a5d126 to your computer and use it in GitHub Desktop.
To get python datetime object from string of datetime for the given format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def get_datetime_from_string(datetime_string, datetime_format='%Y-%m-%d %H:%M:%S'): | |
""" | |
if datetime string includes milliseconds (e.g. '2022-12-06 11:22:23.222') | |
then the datetime format will be '%Y-%m-%d %H:%M:%S.%f' to include fraction | |
""" | |
return datetime.datetime.strptime(datetime_string, datetime_format) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment