Better date and time handling with Arrow

Better date and time handling with Arrow

In this article we’ll have a look at the Arrow library. Arrow markets itself as Better dates and times for Python. Where the standard library has has a series of modules for handeling dates and times, Arrow offers a refreshing new take on things.

Basic Arrow-ing

Arrow is designed as a drop-in replacement for the datetime module from the standard python library. This means that you can replace any datetime objects with an Arrow object.

If you have use any Python modules, functions or objects that expect a date  object, you will have to convert the Arrow  object accordingly:

Parsing and Formatting Arrow objects

Creating Arrow objects from a string and formatting existing arrow objects:

Even humanizing your Arrow objects is straightforward with Arrow. The included locales support makes it possible to translate your output to a multitude of languages:

Date calculations

Calculating new dates is very easy with Arrow. Simply use the replace  function to create a new Arrow  object. The keyword you supply to replace  determines whether the new datetime is modified or shifted:

Our first invocation to replace  used the singular hour, which set the hour attribute of our new Arrow  object to 5 hours. The second invocation specified the keyword hours, indicated that we want to add 5 hours to the existing value.

The replace  method can also be used to do timezone conversions:

Date ranges and spans

One of my favourite features of Arrow is the Ranges & spans functionality. In a number of projects this has been very useful:

Leave a Reply

Your email address will not be published. Required fields are marked *