Tutorials  /  Python

Python Current Date Time - Tutorial

Ccentron Redaktion · February 2024 ·4 min read ·Python, Tutorial

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

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

To retrieve the current date and time, use the datetime.now() function:

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

Output: 2025-01-21 10:17:56.456080

Note: datetime.now() returns a naive datetime object that does not include timezone information. For applications that require time zones, you should use the zoneinfo library.

Python Current Date

If you're only interested in the date, you can use the date() method of the datetime object:

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

Output: 2025-01-21

Python Current Time

If you only need the time, use the time() method of the datetime object:

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

Output: 14:19:46.423440

Time Zones with the zoneinfo Module

Since Python 3.9, the zoneinfo module is part of the standard library and allows you to work with time zones based on the IANA time zone database.

To get the current time in a specific time zone, you can do the following:

from datetime import datetime

from zoneinfo import ZoneInfo # Available from Python 3.9

# Define time zones

utc = ZoneInfo("UTC")

pst = ZoneInfo("America/Los_Angeles")

ist = ZoneInfo("Asia/Kolkata")

# Current date and time in different time zones

now_utc = datetime.now(tz=utc)

now_pst = datetime.now(tz=pst)

now_ist = datetime.now(tz=ist)

print('Current Date and Time in UTC:', now_utc)

print('Current Date and Time in PST:', now_pst)

print('Current Date and Time in IST:', now_ist)

Code

Output:

Current Date and Time in UTC: 2025-01-21 07:12:16.886000+00:00

Current Date and Time in PST: 2025-01-21 00:12:16.886000-08:00

Current Date and Time in IST: 2025-01-21 12:42:16.886000+05:30

Using the pytz Module for Time Zones

Let’s see some examples of using the pytz module to get the time in specific time zones:

import pytz

from datetime import datetime

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(tz=pst))

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

Code

Output:

Code
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

Note: The zoneinfo module uses the system’s time zone data. On some systems, especially Windows, these data may not be available. In such cases, you can install the tzdata package from PyPI to provide the necessary time zone data.

Python Pendulum Module

Pendulum is another library for easy handling of dates and times in Python. It offers a user-friendly API and handles time zones correctly.

You can install Pendulum with the following command:

Console
pip install pendulum

An example of using Pendulum to work with time zones:

import pendulum

utc = pendulum.timezone('UTC')

pst = pendulum.timezone('America/Los_Angeles')

ist = pendulum.timezone('Asia/Calcutta')

print('Current Date Time in UTC =', pendulum.now(utc))

print('Current Date Time in PST =', pendulum.now(pst))

print('Current Date Time in IST =', pendulum.now(ist))

Output:

Code
Current Date Time in UTC = 2025-01-20 09:07:20.267774+00:00 
Current Date Time in PST = 2025-01-20 02:07:20.267806-07:00 
Current Date Time in IST = 2025-01-20 14:37:20.267858+05:30

Important: Since version 3.0.0, Pendulum only supports Python versions 3.8 and above. Ensure your Python version is compatible.

%3Cscript%20type%3D%22application%2Fld%2Bjson%22%3E%0D%0A%7B%0D%0A%20%20%22%40context%22%3A%20%22https%3A%2F%2Fschema.org%22%2C%0D%0A%20%20%22%40type%22%3A%20%22HowTo%22%2C%0D%0A%20%20%22name%22%3A%20%22Python%20Current%20Date%20and%20Time%3A%20Tutorial%22%2C%0D%0A%20%20%22description%22%3A%20%22This%20guide%20explains%20how%20to%20get%20the%20current%20date%20and%20time%20in%20Python%20using%20datetime%2C%20extract%20only%20the%20date%20or%20only%20the%20time%2C%20and%20work%20with%20time%20zones%20using%20zoneinfo%2C%20pytz%2C%20or%20Pendulum.%22%2C%0D%0A%20%20%22inLanguage%22%3A%20%22en-US%22%2C%0D%0A%20%20%22tool%22%3A%20%5B%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTool%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Python%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTool%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22datetime%20module%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTool%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22zoneinfo%20module%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTool%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22pytz%20module%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTool%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Pendulum%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTool%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22pip%22%0D%0A%20%20%20%20%7D%0D%0A%20%20%5D%2C%0D%0A%20%20%22supply%22%3A%20%5B%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSupply%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Python%20installation%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSupply%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Python%203.9%20or%20newer%20for%20zoneinfo%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSupply%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Optional%3A%20tzdata%20package%20for%20systems%20without%20timezone%20data%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSupply%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Optional%3A%20pytz%20library%22%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSupply%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Optional%3A%20Pendulum%20library%22%0D%0A%20%20%20%20%7D%0D%0A%20%20%5D%2C%0D%0A%20%20%22step%22%3A%20%5B%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSection%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Get%20the%20current%20date%20and%20time%20with%20datetime%22%2C%0D%0A%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Import%20datetime%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Import%20the%20datetime%20class%20with%20from%20datetime%20import%20datetime.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Print%20the%20current%20date%20and%20time%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Call%20datetime.now%28%29%20and%20print%20the%20result%20with%20print%28datetime.now%28%29%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTip%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22datetime.now%28%29%20returns%20a%20naive%20datetime%20object%20that%20does%20not%20include%20timezone%20information.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSection%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Display%20only%20the%20current%20date%22%2C%0D%0A%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Store%20the%20current%20datetime%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Store%20the%20current%20datetime%20in%20a%20variable%2C%20for%20example%20current%20%3D%20datetime.now%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Extract%20the%20date%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Use%20the%20date%28%29%20method%20of%20the%20datetime%20object%2C%20for%20example%20today%20%3D%20current.date%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%223%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Print%20the%20date%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Print%20the%20date%20with%20print%28today%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSection%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Display%20only%20the%20current%20time%22%2C%0D%0A%20%20%20%20%20%20%22position%22%3A%20%223%22%2C%0D%0A%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Extract%20the%20time%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Use%20the%20time%28%29%20method%20of%20the%20datetime%20object%2C%20for%20example%20current_time%20%3D%20current.time%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Print%20the%20time%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Print%20the%20current%20time%20with%20print%28current_time%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSection%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Use%20time%20zones%20with%20zoneinfo%22%2C%0D%0A%20%20%20%20%20%20%22position%22%3A%20%224%22%2C%0D%0A%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Import%20zoneinfo%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Import%20ZoneInfo%20with%20from%20zoneinfo%20import%20ZoneInfo.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTip%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22The%20zoneinfo%20module%20has%20been%20part%20of%20the%20Python%20standard%20library%20since%20Python%203.9.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Define%20time%20zones%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Define%20the%20required%20time%20zones%2C%20for%20example%20ZoneInfo%28%5C%22UTC%5C%22%29%2C%20ZoneInfo%28%5C%22America%2FLos_Angeles%5C%22%29%2C%20and%20ZoneInfo%28%5C%22Asia%2FKolkata%5C%22%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%223%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Get%20the%20current%20time%20for%20each%20time%20zone%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Call%20datetime.now%28tz%3Dyour_timezone%29%20to%20get%20the%20current%20date%20and%20time%20in%20the%20selected%20time%20zone.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%224%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Print%20timezone-aware%20values%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Print%20the%20results%20for%20UTC%2C%20PST%2C%20or%20IST%20with%20print%28%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTip%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22On%20some%20systems%2C%20especially%20Windows%2C%20timezone%20data%20may%20be%20missing.%20In%20that%20case%2C%20install%20the%20tzdata%20package%20from%20PyPI.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSection%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Use%20time%20zones%20with%20pytz%22%2C%0D%0A%20%20%20%20%20%20%22position%22%3A%20%225%22%2C%0D%0A%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Import%20pytz%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Import%20pytz%20with%20import%20pytz.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Define%20pytz%20time%20zones%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Define%20time%20zones%20such%20as%20pytz.utc%2C%20pytz.timezone%28%27America%2FLos_Angeles%27%29%2C%20or%20pytz.timezone%28%27Asia%2FCalcutta%27%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%223%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Print%20the%20current%20time%20with%20pytz%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Use%20datetime.now%28tz%3Dtimezone%29%20to%20print%20the%20current%20time%20in%20the%20selected%20pytz%20time%20zone.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTip%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22For%20new%20projects%2C%20zoneinfo%20is%20often%20the%20preferred%20option%20because%20it%20is%20part%20of%20the%20standard%20library%20starting%20with%20Python%203.9.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%2C%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%22%40type%22%3A%20%22HowToSection%22%2C%0D%0A%20%20%20%20%20%20%22name%22%3A%20%22Use%20Pendulum%20for%20date%2C%20time%2C%20and%20time%20zones%22%2C%0D%0A%20%20%20%20%20%20%22position%22%3A%20%226%22%2C%0D%0A%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Install%20Pendulum%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Install%20Pendulum%20with%20pip%20install%20pendulum.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToTip%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Since%20version%203.0.0%2C%20Pendulum%20supports%20only%20Python%20versions%203.8%20and%20newer.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%222%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Import%20Pendulum%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Import%20Pendulum%20with%20import%20pendulum.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%223%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Define%20Pendulum%20time%20zones%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Define%20time%20zones%20with%20pendulum.timezone%28%27UTC%27%29%2C%20pendulum.timezone%28%27America%2FLos_Angeles%27%29%2C%20or%20pendulum.timezone%28%27Asia%2FCalcutta%27%29.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%2C%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToStep%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%224%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Print%20time%20values%20with%20Pendulum%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%22itemListElement%22%3A%20%5B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%40type%22%3A%20%22HowToDirection%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22position%22%3A%20%221%22%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22Print%20the%20current%20time%20for%20the%20defined%20time%20zones%20to%20conveniently%20handle%20date%20and%20time%20values%20with%20timezone%20awareness.%22%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%5D%0D%0A%20%20%20%20%7D%0D%0A%20%20%5D%0D%0A%7D%0D%0A%3C%2Fscript%3E

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren