{"id":6155,"date":"2021-05-16T10:43:26","date_gmt":"2021-05-16T05:13:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=6155"},"modified":"2021-11-22T18:42:37","modified_gmt":"2021-11-22T13:12:37","slug":"python-string-to-datetime-or-date-object","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-to-datetime-or-date-object\/","title":{"rendered":"Python: String to datetime or date object"},"content":{"rendered":"
We will see in this article how to convert different format string to a DateTime or date object. We will do so using a function provided by Python\u2019s date-time module to change string into a datetime object i.e. datetime.strptime(datetime_str, format):<\/em><\/p>\n Arguments:<\/p>\n The function will return the\u00a0 date time object when the right parameters are provided. In case there is something wrong with the parameters provided to the function will throw a value error.<\/p>\n We can obtain the date time in our desired format by passing in the correct format code as arguments.<\/p>\n To achieve the \u2018MM\/DD\/YY HH:MM:SS \u2018 format we are goin to change the order of format from the previous code.<\/p>\n In case we pass a format that is not compatible with the with the function, it throws a ValueError.<\/p>\n We can handle that error beforehand so that the program execution does not stops.<\/p>\n (For correct format)<\/p>\n (For wrong format)<\/p>\n In case we have the millisecond info,<\/p>\n To add the time zone in the format we have to include %z<\/p>\n To convert a datetime string into a date object we have to first convert it into a datetime object using To convert a datetime string into a time object we have to first convert it into a datetime object using <\/p>\n","protected":false},"excerpt":{"rendered":" String to datetime or date object in Python. We will see in this article how to convert different format string to a DateTime or date object. We will do so using a function provided by Python\u2019s date-time module to change string into a datetime object i.e. strptime( ) Syntax- datetime.strptime(datetime_str, format): Arguments: datetime_str – It …<\/p>\nstrptime( )<\/code><\/p>\n
Syntax-<\/pre>\n
\n
Convert string (\u2018DD\/MM\/YY HH:MM:SS \u2018) to datetime object :<\/h3>\n
\n
from datetime import datetime\r\ndateTimeString = '15\/5\/21 11:12:13'\r\n# Converting the String ( \u2018DD\/MM\/YY HH:MM:SS \u2018) into a datetime object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%d\/%m\/%y %H:%M:%S')\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n2021-05-15 11:12:13<\/pre>\n
Convert string (\u2018MM\/DD\/YY HH:MM:SS \u2018) to datetime object :<\/h3>\n
from datetime import datetime\r\ndateTimeString = '5\/15\/2021 11:12:13'\r\n#Converting the String ( \u2018MM\/DD\/YY HH:MM:SS\u2018) into a datetime object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%m\/%d\/%Y %H:%M:%S')\r\nprint(dateTimeObj)<\/pre>\n
Output :\r\n2021-05-15 11:12:13<\/pre>\n
Convert string to datetime and handle ValueError :<\/h3>\n
from datetime import datetime\r\ndateTimeString = '5\/15\/2021 11:12:13'\r\n#Converting the String ( \u2018MM\/DD\/YY HH:MM:SS\u2018) into a datetime object\r\ntry:\r\n dateTimeObj = datetime.strptime(dateTimeString, '%m\/%d\/%Y %H:%M:%S')\r\n print(dateTimeObj)\r\nexcept ValueError as e:\r\n print(e)\r\n<\/pre>\n
Output :\r\n2021-05-15 11:12:13<\/pre>\n
from datetime import datetime\r\ndateTimeString = '5\/15\/2021 11:12:13'\r\n#Converting the String ( \u2018MM\/DD\/YY HH:MM:SS\u2018) into a datetime object\r\ntry:\r\n dateTimeObj = datetime.strptime(dateTimeString, '%d-%m-%Y %H:%M:%S')\r\n print(dateTimeObj)\r\nexcept ValueError as e:\r\n print(e)\r\n<\/pre>\n
Output :\r\ntime data '5\/15\/2021 11:12:13' does not match format '%d-%m-%Y %H:%M:%S'<\/pre>\n
Python: Convert string to datetime \u2013 ( string format yyyy-mm-dd hh-mm-ss) :<\/h3>\n
from datetime import datetime\r\ndateTimeString = '2021-5-15 11-12-13'\r\n#Converting the String ( \u2018yyyy-mm-dd hh-mm-ss\u2018) into a datetime object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%Y-%m-%d %H-%M-%S')\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n2021-05-15 11:12:13<\/pre>\n
Python: Convert string to datetime \u2013 ( string format MMM DD YYYY HH:MM:SS) :<\/h3>\n
from datetime import datetime\r\ndateTimeString = 'May 15 2021 11:12:13'\r\n#Converting the String ( \u2018MMM DD YYYY HH:MM:SS\u2018) into a datetime object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%b %d %Y %H:%M:%S')\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n2021-05-15 11:12:13<\/pre>\n
Python: Convert string to datetime with milliseconds- ( string format DD\/MM\/YY HH:MM:SS:FFFFFF) :<\/h3>\n
from datetime import datetime\r\ndateTimeString = '15\/5\/21 11:12:13.453'\r\n#Converting the String ( \u2018DD\/MM\/YY HH:MM:SS:FFFFFF\u2018) into a datetime object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%d\/%m\/%y %H:%M:%S.%f')\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n2021-05-15 11:12:13.453000<\/pre>\n
Python: Convert string to datetime with timezone :<\/h3>\n
from datetime import datetime\r\ndateTimeString = '15\/5\/21 11:12:13+05:30'\r\n#Converting the String wih timezone into a datetime object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%d\/%m\/%y %H:%M:%S%z')\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n2021-05-15 11:12:13+05:30<\/pre>\n
Python: Convert string to date object :<\/h3>\n
strptime( )<\/code> and then pass it into the date( )<\/em> function to get our date object.<\/p>\n
from datetime import datetime\r\ndateTimeString = '2021-5-15'\r\n#Converting the String into a date object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%Y-%m-%d').date()\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n2021-05-15<\/pre>\n
Python: Convert string to time object :<\/h3>\n
strptime( )<\/code> and then pass it into the time( )<\/em> function to get our time object.<\/p>\n
from datetime import datetime\r\ndateTimeString = '11:12:13'\r\n#Converting the String into a time object\r\ndateTimeObj = datetime.strptime(dateTimeString, '%H:%M:%S').time()\r\nprint(dateTimeObj)\r\n<\/pre>\n
Output :\r\n11:12:13<\/pre>\n