Skip to content

Instantly share code, notes, and snippets.

@chandra-prakash-meghwal
Created December 6, 2022 05:58
Show Gist options
  • Save chandra-prakash-meghwal/7a5fc9122b2542aa5806f7dcb7a5d126 to your computer and use it in GitHub Desktop.
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
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