Convert the timestamp value in Python
In this article, TipsMake.com will show you how to convert the timestamp value into a datetime object and a datetime object into a timestamp value in Python, along with specific examples to make it easier to visualize and capture this value. better.
In databases, storing dates and times as timestamp values is quite common.
Unix timestamp or Unix time is the system that expresses a point on the time axis, using the number of seconds to determine the time, with the starting point from 00:00:00 on January 1, 1970 in UTC time.
Example: At 13:54:27 - 21/05/2019 has a timestamp value of 1558446867; This means from 00:00 to 1/1/1970 to 13:54:27 - May 21, 2019 is 1558446867 seconds.
Example 1: Convert timestamp value to datetime
from datetime import datetime timestamp = 1562907183 dt_object = datetime.fromtimestamp(timestamp) print("dt_object =", dt_object) print("type(dt_object) =", type(dt_object))
Run the program, the result returns the date and time corresponding to the timestamp 1562907183 value :
dt_object = 2019-07-12 11:53:03 type(dt_object) =
Here, we import the datetime class from the datetime module and then use the datetime.fromtimestamp () method to return the local date and time ( datetime object ) stored in the dt_object variable .
You can easily create a time display string using the strftime method ().
Example 2: Convert datetime to timestamp value
You can retrieve the timestamp value from a datetime object using the method datetime.timestamp ().
from datetime import datetime # ngay gio hien tai now = datetime.now() timestamp = datetime.timestamp(now) print("timestamp =", timestamp) print("Ngay gio hien tai:", now)
Result:
timestamp = 1562915824.395594 Ngay gio hien tai: 2019-07-12 14:17:04.395594
Previous lesson: Current date and time in Python
Next lesson: Module time in Python
You should read it
May be interested
- Bookmark 5 best Python programming learning websitesif you are a developer or you are studying and want to stick with this industry, learn python to add a highlight in your journey.
- For in Python loopin this article, we will learn more about for for loop in python as well as its variations, how to use for to repeat a string of elements in python such as list, string or other iterative objects.
- Manage files and folders in Pythonpython also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
- Multiple choice quiz about Python - Part 3today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
- 5 choose the best Python IDE for youin order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
- What is Python? Why choose Python?python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages for people who are first exposed to programming languages.
- Python data type: string, number, list, tuple, set and dictionaryin this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
- How to install Python on Windows, macOS, Linuxto get started with python, you first need to install python on the computer you are using, be it windows, macos or linux. below is a guide to installing python on your computer, specific to each operating system.
- How to set up Python to program on WSLget started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
- Multiple choice quiz about Python - Part 4continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.