Python Pendulum create new DateTime instances

This Python Pendulum tutorial to show you how to instantiate new DateTime instances using the Pendulum package.

Table of contents

  1. Install Pendulum package
  2. Create new DateTime from year, month, day
  3. Create new DateTime from , year, month, day, hour, minute, second
  4. Create new DateTime with local timezone
  5. Create new DateTime with given timezone
  6. Create DateTime using now(), yesterday(), today(), tomorrow() methods
  7. Create DateTime without timezone using naive() method
  8. Create DateTime using from_timestamp() method
  9. Create DateTime instance from datetime.datetime using instance() method

Install Pendulum package

Installing the Pendulum package using the below command.

pip install pendulum

Create new DateTime from year, month, day

The below program we use the datetime() method to create new DateTime object from given year, month and day. By default the created object has UTC timezone.

import pendulum

# Create DateTime with year, month, day
dt = pendulum.datetime(2021, 10, 15)

print(dt)
print(dt.timezone.name)
The program output.
2021-10-15T00:00:00+00:00
UTC

Create new DateTime from , year, month, day, hour, minute, second

The below program we use the datetime() method to create new DateTime object from given year, month, day and hour, minute, second.

import pendulum

# Create DateTime with year, month, day, hour, minute, second
dt = pendulum.datetime(2021, 10, 15, 8, 20, 50)

print(dt)
print(dt.timezone.name)
The program output.
2021-10-15T08:20:50+00:00
UTC

Create new DateTime with local timezone

To create DateTime object which has local timezone we can use local() method as following program.

import pendulum

# Create DateTime with year, month, day, hour, minute, second
dt = pendulum.local(2021, 10, 20, 9, 30, 40)

print(dt)
print(dt.timezone.name)
The program output.
2021-10-20T09:30:40+07:00
Asia/Bangkok

Create new DateTime with given timezone

The below program to show how we can create DateTime object with a provided timezone.

import pendulum 

dt = pendulum.datetime(2021, 9, 20, tz='America/Toronto')

print(dt)
print(dt.timezone.name)
The program output.
2021-09-20T00:00:00-04:00
America/Toronto

We also can create DateTime from a provided Timezone object as below.

import pendulum

paris_timezone = pendulum.timezone('Europe/Paris')
dt = pendulum.datetime(2021, 10, 20, tz=paris_timezone)

print(dt)
print(dt.timezone.name)
The program output.
2021-10-20T00:00:00+02:00
Europe/Paris

Create DateTime using now(), yesterday(), today(), tomorrow() methods

In the Python program below we will explore following methods.

  • now() to create DateTime instance of current date and time.
  • yesterday() to create DateTime instance of today.
  • today() to create DateTime instance of yesterday.
  • tomorrow() to create DateTime instance of tomorrow.

import pendulum

now = pendulum.now()
yesterday = pendulum.yesterday()
today = pendulum.today()
tomorrow = pendulum.tomorrow()

print(now)
print(yesterday)
print(today)
print(tomorrow)
The program output.
2021-12-11T17:39:54.056719+07:00
2021-12-10T00:00:00+07:00
2021-12-11T00:00:00+07:00
2021-12-12T00:00:00+07:00

We can also create new DateTime instance with these method with a provided timezone.

import pendulum

now_london = pendulum.now(tz='Europe/London')
yesterday_paris = pendulum.yesterday(tz='Europe/Paris')
today_toronto = pendulum.today(tz='America/Toronto')
tomorrow_alaska = pendulum.tomorrow(tz='US/Alaska')

print(now_london)
print(yesterday_paris)
print(today_toronto)
print(tomorrow_alaska)
The program output.
2021-12-11T10:47:41.039078+00:00
2021-12-10T00:00:00+01:00
2021-12-11T00:00:00-05:00
2021-12-12T00:00:00-09:00

Create DateTime without timezone using naive() method

In previous methods we create DateTime instances with a specific timezone or by default Pendulum create DateTime instance with UTC timezone. In this section we learn how to create DateTime instance without timezone using the naive() method.

import pendulum

dt = pendulum.naive(2021, 10, 20)

print(dt)
print(dt.timezone)
The program output.
2021-10-20T00:00:00
None

Create DateTime using from_timestamp() method

The following Python program to show you how to create a DateTime instance from a given timestamp with default timezone is UTC.

import pendulum

dt = pendulum.from_timestamp(-1)

print(dt)
print(dt.timezone)
The program output.
1969-12-31T23:59:59+00:00
Timezone('UTC')

We also provide timezone value as below.

import pendulum

dt = pendulum.from_timestamp(1000, tz='Europe/Paris')

print(dt)
print(dt.timezone)
The program output.
1970-01-01T01:16:40+01:00
Timezone('Europe/Paris')

Create DateTime instance from datetime.datetime using instance() method

The following Python program to show you how to create a new Pendulum DateTime instance from a given datetime.datetime instance.

import datetime
import pendulum


dt = datetime.datetime(2021, 1, 20)
dt_p = pendulum.instance(dt)

print(dt)
print(dt_p)
print(dt_p.timezone)
The program output.
2021-01-20 00:00:00
2021-01-20T00:00:00+00:00
Timezone('UTC')

Happy Coding 😊