{"id":6315,"date":"2023-10-30T16:31:35","date_gmt":"2023-10-30T11:01:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=6315"},"modified":"2023-11-10T12:07:12","modified_gmt":"2023-11-10T06:37:12","slug":"python-how-to-convert-a-timestamp-string-to-a-datetime-object-using-datetime-strptime","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-convert-a-timestamp-string-to-a-datetime-object-using-datetime-strptime\/","title":{"rendered":"Python: How to convert a timestamp string to a datetime object using datetime.strptime()"},"content":{"rendered":"
In this tutorial, we will learn how to convert a timestamp string to a datetime object using datetime.strptime(). Also, you can understand how to to create a datetime object from a string in Python with examples below.<\/p>\n
The Datetime module provides a datetime class that has a method to convert string to a datetime object.<\/p>\n So in the above syntax, you can see that it accepts a string containing a timestamp. It parses the string according to format codes and returns a datetime object created from it.<\/p>\n First import datetime class from datetime module to use this,<\/p>\n Also Read:<\/span><\/p>\n In the In the accordance with the string and format code used, the method returns its equivalent datetime object.<\/p>\n Let’s see the following example, to understand how it works:<\/p>\n <\/p>\n where,<\/p>\nstrptime()<\/code>method generates a datetime object from the given string.<\/p>\n
Syntax:<\/h3>\n
datetime.strptime(date_string, format)<\/pre>\n
from datetime import datetime<\/code><\/p>\n
\n
<\/a>Complete Format Code List<\/h2>\n
\n\n
\n Format Codes<\/th>\n Description<\/th>\n Example<\/th>\n<\/tr>\n \n %d<\/td>\n Day of the month as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 31<\/td>\n<\/tr>\n \n %a<\/td>\n Weekday as the abbreviated name<\/td>\n Sun, Mon, \u2026, Sat<\/td>\n<\/tr>\n \n %A<\/td>\n Weekday as full name<\/td>\n Sunday, Monday, \u2026, Saturday<\/td>\n<\/tr>\n \n %m<\/td>\n Month as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 12<\/td>\n<\/tr>\n \n %b<\/td>\n Month as an abbreviated name<\/td>\n Jan, Feb, \u2026, Dec<\/td>\n<\/tr>\n \n %B<\/td>\n Month as full name<\/td>\n January, February, \u2026, December<\/td>\n<\/tr>\n \n %y<\/td>\n A Year without century as a zero-padded decimal number<\/td>\n 00, 01, \u2026, 99<\/td>\n<\/tr>\n \n %Y<\/td>\n A Year with a century as a decimal number<\/td>\n 0001, \u2026, 2018, \u2026, 9999<\/td>\n<\/tr>\n \n %H<\/td>\n Hour (24-hour clock) as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 23<\/td>\n<\/tr>\n \n %M<\/td>\n Minute as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 59<\/td>\n<\/tr>\n \n %S<\/td>\n Second as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 59<\/td>\n<\/tr>\n \n %f<\/td>\n Microsecond as a decimal number, zero-padded on the left<\/td>\n 000000, 000001, \u2026, 999999<\/td>\n<\/tr>\n \n %I<\/td>\n Hour (12-hour clock) as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 12<\/td>\n<\/tr>\n \n %p<\/td>\n Locale\u2019s equivalent of either AM or PM<\/td>\n AM, PM<\/td>\n<\/tr>\n \n %j<\/td>\n Day of the year as a zero-padded decimal number<\/td>\n 01, 02, 03, 04 \u2026, 366<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/a>How strptime() works?<\/h2>\n
strptime()<\/code>class method, it takes two arguments:<\/p>\n
\n