- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert timestamp string to datetime object in Python?
In this article, we will show you how to convert a timestamp string to DateTime object in Python. Below are the various methods to accomplish this task −
Using datetime.fromtimestamp() function
Using datetime.fromtimestamp() & strftime
Using datetime.strptime()
Convert timestamp to a date time object with format codes with mixed text
Using datetime.fromtimestamp() function
To obtain a date from a UNIX timestamp, use the datetime module's fromtimestamp() function. This function accepts a timestamp as input and returns the datetime object corresponding to that timestamp.
Syntax
fromtimestamp(timestamp, tz=None)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword to import datetime from datetime module
Create a variable to store the input timestamp string
Use the datetime.fromtimestamp() function to convert the input timestamp to datetime object.
Print resultant datetime object.
Use the type() function (returns the data type of an object) to print the type of resultant datetime object.
Example
The following program converts the timestamp to datetime object using datetime.fromtimestamp() function −
# importing datetime from datetime module from datetime import datetime # input timestamp timestamp_string = 1345612072 # Converting timestamp to DateTime object datetime_object = datetime.fromtimestamp(timestamp_string) # printing resultant datetime object print("Resultant datetime object:",datetime_object) # printing the type of resultant datetime object print("Type of datetime object:",type(datetime_object))
Output
On executing, the above program will generate the following output −
Resultant datetime object: 2012-08-22 05:07:52 Type of datetime object:
Using datetime.fromtimestamp() & strftime
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the datetime.fromtimestamp() function to convert the timestamp string to datetime object and format it using the strftime() method (returns a string that represents a datetime object based on the format codes).
strftime(format)
Print the resultant DateTime object.
Example
The following program converts the timestamp to datetime object using datetime.fromtimestamp() function and format it using strftime −
# importing datetime from datetime module from datetime import datetime # input timestamp timestamp_string = 1345612072 # Converting timestamp string to datetime object and formatting it datetime_object = datetime.fromtimestamp(timestamp_string).strftime('%d-%m-%y') # printing resultant datetime object print("Resultant datetime object:",datetime_object) # printing the type of resultant datetime object print("Type of datetime object:", type(datetime_object))
Output
On executing, the above program will generate the following output −
Resultant datetime object: 22-08-12 Type of datetime object: <class 'str'>
strftime function()
The strftime() function returns a formatted date and time. It accepts a format string that you can use to get the result you want. The following are the directives that it supports.
Directive | Meaning |
---|---|
%a | Locale's abbreviated weekday name |
%A | Locale's full weekday name |
%b | Locale's abbreviated month name. |
%B | Locale's full month name. |
%c | Locale's appropriate date and time representation. |
%d | Day of the month as a decimal number [01,31]. |
%H | Hour (24-hour clock) as a decimal number [00,23]. |
%I | Hour (12-hour clock) as a decimal number [01,12]. |
%m | Month as a decimal number [01,12]. |
%M | Minute as a decimal number [00,59]. |
%p | Locale's equivalent of either AM or PM. |
%S | Second as a decimal number [00,61]. |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w | Weekday as a decimal number [0(Sunday),6]. |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x | Locale's appropriate date representation. |
%X | Locale's appropriate time representation. |
%y | Year without century as a decimal number [00,99]. |
%Y | Year with century as a decimal number. |
%Z | Time zone name (no characters if no time zone exists). |
%% | A literal "%" character. |
Using datetime.strptime()
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Create a variable to store the format.
Use the datetime.strptime() function (formats a time stamp in string format into a date-time object) to convert the timestamp to datetime object by passing the input timestamp and format as arguments to it.
Print resultant datetime object.
Example
The following program converts the timestamp to datetime object and format it using datetime.strptime() function −
# importing datetime from datetime module from datetime import datetime # input timestamp string timestamp_string = '2013-06-09T11::12::40.356237' # input format format = '%Y-%m-%dT%H::%M::%S.%f' # converting the timestamp string to datetime object datetime_object = datetime.strptime(timestamp_string, format) # printing the resultant datetime object print(datetime_object) # printing the type of resultant datetime object print(type(datetime_object))
Output
On executing, the above program will generate the following output −
2013-06-09 11:12:40.356237 <class 'datetime.datetime'>
Convert timestamp to a date time object with format codes with mixed text
Example
The following program convert timestamp in the input mixed text string to a date time object and format it using datetime.strptime() function −
# importing datetime from datetime module from datetime import datetime # input text string textString = "I was born on March 14th 2020 at 4 PM" # Converting timestamp in the input mixed text string to # datetime object and formatting it datetime_object = datetime.strptime(textString, 'I was born on %B %dth %Y at %I %p') # printing resultant datetime object print("Resultant datetime object:",datetime_object) # printing the type of resultant datetime object print("Type of datetime object:", type(datetime_object))
Output
On executing, the above program will generate the following output −
Resultant datetime object: 2020-03-14 16:00:00 Type of datetime object: <class 'datetime.datetime'>
Conclusion
We studied several methods in this article to convert a given timestamp to a DateTime object in Python. We also learned how to convert a timestamp containing mixed text to a DateTime object.
Related Tutorials: Python Tutorial, Python Date & Time Tutorial