Skip to main content

A simple and accurate Python library for the Persian Jalali calendar with no external dependencies.

Project description

Persian Jalali Calendar

A simple, accurate, and lightweight Python library for the Persian (Jalali/Shamsi) calendar with no external dependencies.

This library allows for easy conversion between Jalali and Gregorian dates, provides date arithmetic, and offers helpful methods for formatting and date information, all through an intuitive API modeled after Python's built-in datetime module.

Installation

Install the library from PyPI using pip:

pip install persian-jalali-calendar

In-Depth Usage

JalaliDate - The Core Object

The JalaliDate object is the heart of the library.

from jalali_calendar import JalaliDate
import datetime

# --- Creating a Date ---

# 1. From year, month, and day
d = JalaliDate(1404, 4, 13)
print(d)  # Output: 1404-04-13

# 2. From today's system date
today = JalaliDate.today()
print(f"Today is: {today}")

# 3. From a standard Python datetime.date object
g_date = datetime.date(2025, 7, 4)
j_date = JalaliDate.from_gregorian(g_date)
print(f"Gregorian {g_date} is Jalali {j_date}")

Conversion

Seamlessly convert back and forth.

j_date = JalaliDate(1404, 4, 13)
g_date = j_date.to_gregorian()
print(g_date)  # Output: 2025-07-04
print(type(g_date)) # Output: <class 'datetime.date'>

Accessing Properties

Get detailed information about any date.

d = JalaliDate(1403, 1, 1) # Nowruz 1403

# Basic Properties
print(f"Year: {d.year}, Month: {d.month}, Day: {d.day}")

# Weekday Information (Saturday = 0, Friday = 6)
print(f"Weekday Number: {d.weekday()}")    # Output: 4 (Wednesday)
print(f"Weekday Name: {d.weekday_name()}") # Output: چهارشنبه

# Month Information
print(f"Month Name: {d.month_name()}")     # Output: فروردین

# Year Information
print(f"Is Leap Year? {d.is_leap()}")      # Output: True
print(f"Day of Year: {d.day_of_year()}")   # Output: 1
print(f"Week of Year: {d.week_number()}")  # Output: 1

Formatting with strftime

Create custom-formatted strings.

d = JalaliDate(1404, 4, 13)
# %A: Full weekday name, %d: Day, %B: Full month name, %Y: Year
formatted = d.strftime("%A، %d %B %Y")
print(formatted) # Output: جمعه، ۱۳ تیر ۱۴۰۴

Arithmetic and Comparison

Use standard Python operators for date math.

d1 = JalaliDate(1404, 4, 13)
d2 = d1 - datetime.timedelta(days=10)
print(f"10 days before {d1} was {d2}") # Output: 10 days before 1404-04-13 was 1404-04-03

time_diff = d1 - d2
print(f"Difference is {time_diff.days} days") # Output: 10

# All comparison operators work
print(f"Is d1 after d2? {d1 > d2}") # Output: True

JalaliDateTime

For working with both date and time.

from jalali_calendar import JalaliDateTime, JalaliDate
import datetime

# Create from components
dt = JalaliDateTime(1404, 5, 3, 15, 30, 0)
print(dt) # Output: 1404-05-03 15:30:00

# Get the current date and time
now = JalaliDateTime.now()
print(f"Now: {now}")

# Combine existing objects
d = JalaliDate(1399, 11, 22)
t = datetime.time(10, 0)
dt_combined = JalaliDateTime.combine(d, t)
print(dt_combined) # Output: 1399-11-22 10:00:00

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

persian_jalali_calendar-1.0.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

persian_jalali_calendar-1.0.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file persian_jalali_calendar-1.0.3.tar.gz.

File metadata

  • Download URL: persian_jalali_calendar-1.0.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for persian_jalali_calendar-1.0.3.tar.gz
Algorithm Hash digest
SHA256 15b7ea74517c8053fe7b8bc5b7cb58ff8fa585ddf907a20670fd50af320ebe3d
MD5 dd69a535d518d367b2012839f55d08a0
BLAKE2b-256 b7f22c1227069586f47e65251d0fe209738a117cf7f4f6bfb6a274eb60cf7a5c

See more details on using hashes here.

File details

Details for the file persian_jalali_calendar-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for persian_jalali_calendar-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6bbde75ffa80e0ecd5cde71dbd8e74bc2ccd4d3f61d912a9e71f23227b747831
MD5 60145213b054efa4f1f88bba312a13d6
BLAKE2b-256 5db9e72102a1d5e5b2515739ff25cdf40ee44e469fe0c1c2ae42146a785659f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page