A very simple python lib, used to format datetime with *** time ago statement. Javascript version here. timeago.js.
Such as:
just now
12 seconds ago
3 minutes ago
2 hours ago
24 days ago
6 months ago
2 years ago
in 12 seconds
in 3 minutes
in 2 hours
in 24 days
in 6 months
in 2 years
For other languages see below.
pip install timeago# -*- coding: utf-8 -*-
import timeago, datetime
now = datetime.datetime.now() + datetime.timedelta(seconds = 60 * 3.4)
date = datetime.datetime.now()
# locale
print (timeago.format(date, now, 'zh_CN')) # will print `3分钟前`
# input datetime
print (timeago.format(date, now)) # will print 3 minutes ago
# input timedelta
print (timeago.format(datetime.timedelta(seconds = 60 * 3.4))) # will print 3 minutes ago
# input date, auto add time(0, 0, 0)
print (timeago.format(datetime.date(2016, 5, 27), now))
# input datetime formatted string
print (timeago.format('2016-05-27 12:12:03', '2016-05-27 12:12:12')) # will print just now
# inverse two parameters
print (timeago.format('2016-05-27 12:12:12', '2016-05-27 12:12:03')) # will print a whileonly one API format.
Three parameters of method format:
date: the parameter which will be formatted, must be instance ofdatetime/timedeltaor datetime formatted string.now: reference time, must be instance ofdatetimeor datetime formatted string.locale: the locale code, defaulten.
At the time we're speaking, following locale are available:
arbgcadadeelenen_shorteseufa_IRfifrglguj_INhehuin_BGin_HIin_IDisitjakoltmlmynb_NOnlnn_NOplpt_BRpt_PTrorusksv_SEtathtrukvizh_CNzh_TW
- Fork the project
- Create a locale python script called
[name_of_your_locale].pyfollowing the existing other locales. - Add the name of your locale in the Readme (both in MD and in RST) to keep it updated (alphabetically).
- Add test case following the english model
- Create the Pull Request.
For complicated plurals, you can take example on the PL :flag-pl: locale here