Tutorial

Python Current Date Time

Published on August 3, 2022
Default avatar

By Pankaj

Python Current Date Time

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

We can use Python datetime module to get the current date and time of the local system.

from datetime import datetime

# Current date time in local system
print(datetime.now())

Output: 2018-09-12 14:17:56.456080

Python Current Date

If you are interested only in the date of the local system, you can use the datetime date() method.

print(datetime.date(datetime.now()))

Output: 2018-09-12

Python Current Time

If you want only time in the local system, use time() method by passing datetime object as an argument.

print(datetime.time(datetime.now()))

Output: 14:19:46.423440

Python Current Date Time in timezone - pytz

Most of the times, we want the date in a specific timezone so that it can be used by others too. Python datetime now() function accepts timezone argument that should be an implementation of tzinfo abstract base class. Python pytz is one of the popular module that can be used to get the timezone implementations. You can install this module using the following PIP command.

pip install pytz

Let’s look at some examples of using pytz module to get time in specific timezones.

import pytz

utc = pytz.utc
pst = pytz.timezone('America/Los_Angeles')
ist = pytz.timezone('Asia/Calcutta')

print('Current Date Time in UTC =', datetime.now(tz=utc))
print('Current Date Time in PST =', datetime.now(pst))
print('Current Date Time in IST =', datetime.now(ist))

Output:

Current Date Time in UTC = 2018-09-12 08:57:18.110068+00:00
Current Date Time in PST = 2018-09-12 01:57:18.110106-07:00
Current Date Time in IST = 2018-09-12 14:27:18.110139+05:30

If you want to know all the supported timezone strings, you can print this information using the following command.

print(pytz.all_timezones)

It will print the list of all the supported timezones by the pytz module.

Python Pendulum Module

Python Pendulum module is another timezone library and according to its documentation, it is faster than the pytz module. We can install Pendulum module using below PIP command.

pip install pendulum

You can get list of supported timezone strings from pendulum.timezones attribute. Let’s look into some examples of getting current date and time information in different time zones using the pendulum module.

import pendulum

utc = pendulum.timezone('UTC')
pst = pendulum.timezone('America/Los_Angeles')
ist = pendulum.timezone('Asia/Calcutta')

print('Current Date Time in UTC =', datetime.now(utc))
print('Current Date Time in PST =', datetime.now(pst))
print('Current Date Time in IST =', datetime.now(ist))

Output:

Current Date Time in UTC = 2018-09-12 09:07:20.267774+00:00
Current Date Time in PST = 2018-09-12 02:07:20.267806-07:00
Current Date Time in IST = 2018-09-12 14:37:20.267858+05:30

You can checkout complete python script and more Python examples from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
November 17, 2021

Uups, I need to setup something because after the “succesfull” installation of pytz and pendulum I get the same error: ModuleNotFoundError: No module named ‘pytz’ or ModuleNotFoundError: No module named ‘pendulum’ but when I try to run the install again I got the message “Requirement already satisfied: python-dateutil=2.6 in /usr/lib/python3/dist-packages (from pendulum) (2.7.3)”, similar with pytz module. Please give me a tip for fixing this. I have lubuntu 20.04 and Python 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] on linux

- Juan Manuel Carrillo Campos

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 27, 2021

    Hello. I want to make print current time like mysql’s datetime format. example 2021-08-27 18:10:11 How to make it without 3rd party libraries?

    - yolu

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 27, 2021

      not working for me idk why

      - Foka

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Get our biweekly newsletter

        Sign up for Infrastructure as a Newsletter.

        Hollie's Hub for Good

        Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

        Become a contributor

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        Welcome to the developer cloud

        DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

        Learn more
        DigitalOcean Cloud Control Panel