How to Get the Current Date and Time in Python
![python_tutorials](https://www.deeplearningdaily.com/wp-content/uploads/2020/09/python-600x330.jpeg)
Introduction
Logging, saving records to the database, and accessing files are all common tasks a programmer works on. In each of those cases, date and time play an important role in preserving the meaning and integrity of the data. Programmers often need to engage with date and time.
In this article we will learn how to get the current date and time using Python’s builtin datetime module. With that module, we can get all relevant data in one object, or extract the date and time separately.
We will also learn how to adjust our date and time for different timezones. Finally, we’ll look at converting datetime
objects to the popular Unix or Epoch timestamps.
Getting the Current Date and Time with datetime
The datetime
module contains various classes to get information about the date and time:
datetime.date
: Stores the day, month and year or a datedatetime.time
: Stores the time in hours, minutes, seconds, and microseconds. This information is independent from any datedatetime.datetime
: Stores bothdate
andtime
attributes
Let’s get the current date and time with a datetime.datetime
object, as it is easier to extract date
and time
objects from it. First,