Moment

0.12.1 · deprecated · verified Thu Apr 16

Moment is a Python library designed for simplifying date and time manipulation, drawing inspiration from Moment.js and Kenneth Reitz's Requests library. It provides an intuitive API for parsing, formatting, and performing calculations with dates and times. The library is currently at version 0.12.1, but its GitHub repository shows no activity since 2015, and it is explicitly labeled as deprecated by recent analyses, with recommendations to use more actively maintained alternatives like `arrow` or `pendulum`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create moment objects from current time or strings, perform basic arithmetic operations like adding time, and convert them to standard `datetime` objects. It also highlights the use of `clone()` to handle mutability.

import moment
from datetime import datetime

# Get the current moment (UTC)
now_utc = moment.utcnow()
print(f"Current UTC Moment: {now_utc.format('YYYY-MM-DD HH:mm:ss')}")

# Create a moment from a string
date_str = "2023-10-27 10:30:00"
my_moment = moment.date(date_str)
print(f"Parsed Moment: {my_moment.format('YYYY-MM-DD HH:mm:ss')}")

# Add some time
future_moment = my_moment.add(hours=2, minutes=15)
print(f"Future Moment: {future_moment.format('YYYY-MM-DD HH:mm:ss')}")

# Convert to a native datetime object
native_datetime = future_moment.date
print(f"Native datetime: {native_datetime}")

# Clone a moment to prevent mutation
original = moment.now()
modified = original.clone().add(days=1)
print(f"Original (should be unchanged): {original.format('YYYY-MM-DD')}")
print(f"Modified (1 day later): {modified.format('YYYY-MM-DD')}")

view raw JSON →