Convertdate Library
The `convertdate` library provides methods and functions for converting dates between various calendar systems, including Gregorian, Hebrew, Islamic, Julian, Mayan, and many others. It is currently at version 2.4.1 and sees a few releases per year, indicating active development.
Common errors
-
ModuleNotFoundError: No module named 'convertdate'
cause The `convertdate` library is not installed in your Python environment or the environment where you are running your code.fixInstall the `convertdate` library using pip: `pip install convertdate` -
ERROR: convertdate 2.2.0 has requirement pytz<2020,>=2014.10, but you'll have pytz 2020.1 which is incompatible.
cause This error occurs due to a dependency conflict where an older version of `convertdate` requires a `pytz` version older than what is currently installed or being installed.fixUpgrade `convertdate` to a newer version that supports the `pytz` version you have (or a more recent one), or try installing `convertdate` with a compatible `pytz` version if an older `convertdate` is strictly necessary. Often, updating `convertdate` resolves this: `pip install --upgrade convertdate` -
ValueError: time data '2023-13-01' does not match format '%Y-%m-%d'
cause This error typically happens when you are trying to convert a date string to a datetime object using `datetime.strptime()`, but the format string provided does not exactly match the input date string's structure or the date components (like month 13) are invalid.fixEnsure the format string passed to `strptime()` precisely matches the input date string. If the date components are invalid, correct the input date. For `convertdate`, ensure you pass valid integer year, month, and day components after successful parsing. Example: `from datetime import datetime; date_str = '2023-12-01'; dt_obj = datetime.strptime(date_str, '%Y-%m-%d'); from convertdate import gregorian; gregorian.to_jd(dt_obj.year, dt_obj.month, dt_obj.day)` -
AttributeError: module 'datetime' has no attribute 'strptime'
cause This error occurs when you import the `datetime` module but then try to call `strptime()` directly on the module itself (`datetime.strptime`) instead of on the `datetime` class within the module (`datetime.datetime.strptime`).fixYou should either import `datetime` as `dt` and use `dt.datetime.strptime()`, or directly import the `datetime` class as `from datetime import datetime` and then use `datetime.strptime()`: `from datetime import datetime; date_object = datetime.strptime('2023-01-01', '%Y-%m-%d')`
Warnings
- breaking Python 3.5 and 3.6 are no longer supported. Ensure your environment uses Python 3.7 or newer.
- breaking Timezone handling internally switched from `pytz.utc` to `datetime.timezone.utc`. While direct `pytz` interaction might not be common, be aware of this change if you were relying on specific `pytz` behaviors or objects interacting with `convertdate`.
- breaking The Persian calendar computation method changed from an algorithmic to an astronomical one. This might lead to different results for dates compared to previous versions.
- breaking Attempting to convert dates before the Mayan epoch will now raise a `ValueError` instead of potentially returning incorrect or undefined results.
- breaking Several calendar modules had their variable names regularized (e.g., `coptic.MONTH_NAMES` was renamed to `coptic.MONTHS`). Code accessing the old variable names will no longer function.
- breaking The Julian converter now uses astronomical notation for pre-Common Era dates (1 BCE is 0, 2 BCE is -1). This is a fundamental change in date representation for these periods.
- gotcha Date conversions return results for noon of the day in question. Be aware that some calendar systems begin the day at sundown, which might require an adjustment in your application logic.
Install
-
pip install convertdate
Imports
- french_republican
from convertdate import french_republican
- hebrew
from convertdate import hebrew
- julian
from convertdate import julian
- holidays
from convertdate import holidays
Quickstart
from convertdate import french_republican
from convertdate import hebrew
from convertdate import julian
# Convert Gregorian date to French Republican
fr_date = french_republican.from_gregorian(2014, 10, 31)
print(f"Gregorian 2014-10-31 in French Republican: {fr_date}")
# Convert Gregorian date to Hebrew
he_date = hebrew.from_gregorian(2014, 10, 31)
print(f"Gregorian 2014-10-31 in Hebrew: {he_date}")
# Generate a Julian month calendar for January 2015
julian_month_calendar = julian.monthcalendar(2015, 1)
print(f"Julian calendar for Jan 2015: {julian_month_calendar}")