Using Arrow for Better Datetime

Learn how to be better at dealing with dates and times in python in a few short minutes. Working with dates and times in python is a lot easier than other languages, but can be convoluted and confusing. Arrow provides a convenient api for working with and manipulating dates and times. See what it takes to get started.
Download HD Version next video: Building a TemplateView

resources

code

import arrow

now = arrow.now()
now
now = arrow.utcnow()
now
type(now)

now.datetime

now.to('US/Eastern')
now.to('Japan')

now.day
now.month
now.year

print now.humanize()
print now.humanize(locale='ja')

now.replace(hour=1)
now.replace(hour=1, minute=30)

now.replace(hour=+3)
now.replace(weeks=+4)

now.replace(weeks=-2)


now.span('hour')
now.replace(hours=+5).span('hour')

now.floor('month')
start = now.replace(months=-1).floor('month')
end = now.replace(months=-1).ceil('month')

start, end = now.replace(months=-1).span('month')

start = now.replace(months=-12)

for r in arrow.Arrow.span_range('month', start, now):
    print r

for r in arrow.Arrow.span_range('month', start, now):
    created_start, created_end = r
    print Task.objects.filter(
        created__range=(created_start.datetime, created_end.datetime)).count()
comments powered by Disqus