In addition to using the DateTime standard library to handle times and dates, there are many third-party open source libraries worth trying out when developing with Python.

1, the Arrow

Arrow is a lightweight Python library that deals specifically with times and dates, providing a reasonable, intelligent way to create, manipulate, format, and convert times and dates, as well as a smart module API that supports many common build scenarios. In short, it allows you to use dates and times more easily and with less code. The design was inspired by moment.js and Requests.

Quick start

$ pip install arrowCopy the code
>>> import arrow >>> UTC = arrow.utcnow() >>> UTC < arrow [2013-05-11t21:23:58.970460 +00:00]> >>> UTC = Utc. Replace (hours=-1) >>> UTC <Arrow [2013-05-11T20:23:58.970460+00:00]> >>> local = UTC. To ('US/Pacific') >>> local <Arrow [2013-05-11T13:23:58.970460-07:00]> >>> arrow.get('2013-05-11T21:23:58.970460+00:00') <Arrow [2013-05-11T21:23:58.970460+00:00]> >>> local.timestamp 1368303838 >>> local.format() '2013-05-11 13:23:58-07:00 '>>> local.format('YYYY-MM-DD HH:mm:ss ZZ') '2013-05-11 13:23:58 -07:00' >>> local.humanize() 'an hour ago' >>> Local.humanize (locale='ko_kr') '1시간 전'Copy the code

2, Delorean

Delorean provides a better abstraction than DateTime and Pytz, making it easier for you to handle time. It has many useful features for dealing with time zones, standardizing time zones or changing from one time zone to another.

Quick start

from datetime import datetime
import pytz

est = pytz.timezone('US/Eastern')
d = datetime.now(pytz.utc)
d = est.normalize(d.astimezone(est))
return dCopy the code
from delorean import Delorean

d = Delorean()
d = d.shift('US/Eastern')
return dCopy the code

3, Pendulum

The native Datetime is adequate for basic cases, but it is often stretched and less intuitive when dealing with more complex use cases. Pendulum builds on the standard library with a more concise and easy-to-use API designed to make Python DateTime easier to use.

Quick start

>>> import pendulum >>> now_in_paris = pendulum. Now ('Europe/Paris') >>> now_in_Paris '2016-07-04T00:49:58.502116+02:00' # Seamless timezone switching >>> now_in_paris.in_timezone('UTC') '2016-07-03T22:49:58.502116+00:00' >>> tomorrow = pendulum.now().add(days=1) >>> last_week = pendulum.now().subtract(weeks=1) >>> if pendulum.now().is_weekend(): ... print('Party! ') 'Party! ' >>> past = pendulum.now().subtract(minutes=2) >>> past.diff_for_humans() >>> '2 minutes ago' >>> delta = past - last_week >>> delta.hours 23 >>> delta.in_words(locale='en') '6 days 23 hours 58 minutes' # Proper handling of datetime normalization >>> pendulum.create(2013, 3, 31, 2, 30, 0, 0, 'Europe/Paris') '2013-03-31T03:30:00+02:00' # 2:30 does not exist (Skipped time) # Proper handling of dst transitions >>> just_before = pendulum.create(2013, 3, 31, 1, 59, 59, 999999, 'Europe/Paris') '2013-03-3t01:59:59.999999 +01:00' >>> just_before.add(microseconds=1)' 2013-03-3t03:00:00 +02:00'Copy the code

4, dateutil

Dateutil is an extension of the DateTime standard library. It supports date parsing in almost all string formats, flexible date calculation, and timely internal data update.

Quick start

>>> from dateutil.relativedelta import *
>>> from dateutil.easter import *
>>> from dateutil.rrule import *
>>> from dateutil.parser import *
>>> from datetime import *
>>> now = parse("Sat Oct 11 17:13:46 UTC 2003")
>>> today = now.date()
>>> year = rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR)[0].year
>>> rdelta = relativedelta(easter(year), today)
>>> print("Today is: %s" % today)
Today is: 2003-10-11
>>> print("Year with next Aug 13th on a Friday is: %s" % year)
Year with next Aug 13th on a Friday is: 2004
>>> print("How far is the Easter of that year: %s" % rdelta)
How far is the Easter of that year: relativedelta(months=+6)
>>> print("And the Easter of that year is: %s" % (today+rdelta))
And the Easter of that year is: 2004-04-11Copy the code

5, my moment

The Python library for date/time is also inspired by moment.js and Requests, and is inspired by the Times Python module.

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 future moment that would otherwise be really difficult moment.date("2 weeks from now") # 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") # 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)Copy the code

6, the When. Py

Provides very user-friendly features to help perform common date and time operations.

Usage