• Stars
    star
    724
  • Rank 60,074 (Top 2 %)
  • Language
    Python
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Python library for dealing with dates

moment

A Python library for dealing with dates/times. Inspired by Moment.js and Kenneth Reitz's Requests library. Ideas were also taken from the Times Python module.

Installation

I would advise that this is beta-quality software. You might be interested in:

Still want to use moment?

pip install moment

Usage

import moment
from datetime import datetime

# Create a moment from a string
moment.date("12-18-2012")

# Create a moment with a specified strftime format
moment.date("12-18-2012", "%m-%d-%Y")

# Moment uses the awesome dateparser library behind the scenes
moment.date("2012-12-18")

# Create a moment with words in it
moment.date("December 18, 2012")

# Create a moment that would normally be pretty hard to do
moment.date("2 weeks ago")

# Create a moment from the current datetime
moment.now()

# The moment can also be UTC-based
moment.utcnow()

# Create a moment with the UTC time zone
moment.utc("2012-12-18")

# Create a moment from a Unix timestamp
moment.unix(1355875153626)

# Create a moment from a Unix UTC timestamp
moment.unix(1355875153626, utc=True)

# Return a datetime instance
moment.date(2012, 12, 18).date

# We can do the same thing with the UTC method
moment.utc(2012, 12, 18).date

# Create and format a moment using Moment.js semantics
moment.now().format("YYYY-M-D")

# Create and format a moment with strftime semantics
moment.date(2012, 12, 18).strftime("%Y-%m-%d")

# Use the special `%^` combo to add a date suffix (1st, 2nd, 3rd, 4th, etc)
moment.date(2012, 12, 18).strftime("%B %-d%^, %Y")

# Update your moment's time zone
moment.date(datetime(2012, 12, 18)).locale("US/Central").date

# Alter the moment's UTC time zone to a different time zone
moment.utcnow().timezone("US/Eastern").date

# Set and update your moment's time zone. For instance, I'm on the
# west coast, but want NYC's current time.
moment.now().locale("US/Pacific").timezone("US/Eastern")

# In order to manipulate time zones, a locale must always be set or
# you must be using UTC.
moment.utcnow().timezone("US/Eastern").date

# You can also clone a moment, so the original stays unaltered
now = moment.utcnow().timezone("US/Pacific")
future = now.clone().add(weeks=2)

Chaining

Moment allows you to chain commands, which turns out to be super useful.

# Customize your moment by chaining commands
moment.date(2012, 12, 18).add(days=2).subtract(weeks=3).date

# Imagine trying to do this with datetime, right?
moment.utcnow().add(years=3, months=2).format("YYYY-M-D h:m A")

# You can use multiple keyword arguments
moment.date(2012, 12, 19).add(hours=1, minutes=2, seconds=3)

# And, a similar subtract example...
moment.date(2012, 12, 19, 1, 2, 3).subtract(hours=1, minutes=2, seconds=3)

# In addition to adding/subtracting, we can also replace values
moment.now().replace(hours=5, minutes=15, seconds=0).epoch()

# And, if you'd prefer to keep the microseconds on your epoch value
moment.now().replace(hours=5, minutes=15, seconds=0).epoch(rounding=False)

# Years, months, and days can also be set
moment.now().replace(years=1984, months=1, days=1, hours=0, minutes=0, seconds=0)

# Also, datetime properties are available
moment.utc(2012, 12, 19).year == 2012

# Including plural ones (since I'm bad at remembering)
moment.now().seconds

# We can also manipulate to preferred weekdays, such as Monday
moment.date(2012, 12, 19).replace(weekday=1).strftime("%Y-%m-%d")

# Or, this upcoming Sunday
moment.date("2012-12-19").replace(weekday=7).date

# We can even go back to two Sundays ago
moment.date(2012, 12, 19).replace(weekday=-7).format("YYYY-MM-DD")

# It's also available as a property
moment.utcnow().weekday

# And, there's an easy way to zero out the hours, minutes, and seconds
moment.utcnow().zero

More Repositories

1

flask_heroku

An easy-to-use Flask template for Heroku.
CSS
447
star
2

dom

Check domain name availability from the command line.
Python
215
star
3

flask-engine

A Flask skeleton for Google App Engine, using html5 boilerplate templating.
Python
177
star
4

fred

Python wrapper for the St. Louis Fed's FRED API.
Python
161
star
5

cracking-the-coding-interview

Python solutions for the book Cracking the Coding Interview
Python
124
star
6

asciifi

Turn your pictures into ASCII art
JavaScript
98
star
7

rottentomatoes

Rotten Tomatoes API for Python
Python
87
star
8

excssive

A drag and drop CSS compressor
JavaScript
58
star
9

markdrop

Drag and drop Markdown previews
JavaScript
48
star
10

pjax_flask

Testing out Flask with PJAX
JavaScript
43
star
11

hn

An easy-to-use CLI for HNSearch's API.
Python
31
star
12

ESPN-Basketball

An outdated ESPN Play-By-Play scraper... Not worth your time. Make your own!
Python
29
star
13

golf

Python
28
star
14

heroku_static

Serve all the things!
HTML
26
star
15

dotfiles

Command line goodness
Shell
19
star
16

cookiecutter-scrapy

A simple cookiecutter template for Scrapy projects
Python
18
star
17

dribbble

A simple Python wrapper for the Dribbble API
Python
10
star
18

mapq

An easy-to-use wrapper for the Mapquest Geocoding API.
Python
10
star
19

tumblr

An API wrapper for working with Tumblr and Tumblr's OAuth
Python
9
star
20

drinkups

Objective-C
9
star
21

beautiful_maps

A page-based iOS app highlighting some of Stamen's awesome maps.
Objective-C
8
star
22

darksky

A simple Python wrapper for the Dark Sky API
Python
7
star
23

blog

Current website.
JavaScript
7
star
24

usps_zipcodes

Scrape ZIP Codes from the USPS for a given city and state list.
Python
6
star
25

relaxml

Converting XML should be easy -- and fast.
Python
5
star
26

leetcode

Scraping Leetcode's GraphQL endpoint with Scrapy
Python
5
star
27

flask_cloud

A Flask template to get up and running with DotCloud.
JavaScript
5
star
28

github.vim

Github color scheme for iTerm vim
Vim Script
5
star
29

playing-with-pulp

Playing around with PuLP in Python
Jupyter Notebook
4
star
30

mcmc

Kruschke "Doing Bayesian Data Analysis" and Gelman/Hill "ARM" in PyMC3
Jupyter Notebook
4
star
31

covid-19

My simple COVID-19 death-to-recovery rate model
Jupyter Notebook
4
star
32

smsified

Sending SMS messages should be dead simple.
Python
4
star
33

zappa_template

An easy-to-use template for Zappa and CoffeeScript on Heroku
JavaScript
4
star
34

old

My personal website.
JavaScript
4
star
35

abhi_ipsum

Lorem ipsum with some panache
JavaScript
4
star
36

xml2dict

Fork of xml2dict project
Python
4
star
37

espn_ncaab

CoffeeScript
3
star
38

bittorrent-challenge

Python solution to the BitTorrent Developer Challenge
Python
3
star
39

random

CoffeeScript
3
star
40

bootmaker

Automated Bootstrap workflow
2
star
41

sinatra_template

A simple Sinatra template
JavaScript
2
star
42

background_color

A small app to help learn Backbone.js
JavaScript
2
star
43

madewithdata

Concept site for exploring data APIs.
JavaScript
2
star
44

stamen_maps

A simple UIWebView used to grab screenshots of Stamen's maps.
Objective-C
2
star
45

amazon-reviewers

Find profiles of reviewers who gave the same rating for a product.
Python
2
star
46

import_screencast

JavaScript
2
star
47

applescripts

Simple AppleScript examples.
2
star
48

import_zach

Because the name was available on PyPI.
Python
2
star
49

rebus_generator

ITA's Rebus Generator Puzzle
Python
2
star
50

abhi.info

Because the domain was available
JavaScript
1
star
51

img64

A service to base64 encode external image links
JavaScript
1
star
52

practical_c

Solutions to exercises in the Practical C book.
C
1
star
53

status

Objective-C
1
star
54

domainsaurus

An easy-to-use mashup connecting Domainr's API with Wordnik's API
JavaScript
1
star
55

tatooine

A map of Tunisia with all marked Star Wars locations.
JavaScript
1
star
56

heroku_relaxml

A gut server that makes dealing with XML a whole lot easier.
JavaScript
1
star
57

popcorndoc.js

My fork of the popcorndoc.js plugin for Popcorn.js
JavaScript
1
star
58

wadsworth_constant

A Python script that returns the Wadsworth Constant for input
Python
1
star
59

zippy

A simple wrapper for the Ziptastic ZIPCode API.
Python
1
star
60

word_dump

Python
1
star
61

xcode_textmate_theme

An XCode 4 color scheme from the theme I use in Vim and TextMate.
1
star
62

simplest

Working with Redis should be simple and Pythonic
Python
1
star
63

cookiecutter-streamlit

A quick cookiecutter template for creating Streamlit sites
Python
1
star
64

boardgames

Find new boardgames to play
Jupyter Notebook
1
star
65

lr

JavaScript
1
star
66

holy_catchphrase

Catchphrases and action words from the Adam West Batman show
JavaScript
1
star
67

epa_pcs_zip_codes

EPA permits to pollute public water sources by ZIP Code.
Python
1
star
68

backbone_zombie_test

A simple Backbone.js testing example using Vows and Zombie.js
CoffeeScript
1
star
69

bootstrap_tutorials

Quick Bootstrap tutorials
JavaScript
1
star