A Ruby library to compute distance of dates in words, with localization support. Originally built for Jekyll.
Main features:
1 week and 2 days ago
, 5 months ago
, in 1 year
Jekyll
projects, available as a Liquid Filter and as a Liquid Tag366 days
becomes 1 year ago
instead of 1 year and 1 day ago
In fact, jekyll-timeago
started as an extension for the Liquid template engine, to be used in Jekyll and Octopress backed sites. But actually, you can use it easily on any Ruby project and even as a tool from the terminal!
Read more about the Jekyll
integration in this section.
Add this line to your application's Gemfile:
gem 'jekyll-timeago'
And then execute:
> bundle install
Or install it yourself as:
> gem install jekyll-timeago
The gem provides the timeago
method:
Jekyll::Timeago.timeago(from, to = Date.today, options = {})
You can include the method in the current context by including the module (so you can call directly the timeago
method without using the whole scope):
include Jekyll::Timeago
Examples:
>> timeago(Date.today)
=> "today"
>> timeago(Date.today.prev_day)
=> "yesterday"
>> timeago(Date.today.prev_day(10))
=> "1 week and 3 days ago"
>> timeago(Date.today.prev_day(100))
=> "3 months and 1 week ago"
>> timeago(Date.today.prev_day(500))
=> "1 year and 4 months ago"
>> timeago('2010-1-1', '2012-1-1')
=> "2 years ago"
>> timeago(Date.today.next_day)
=> "tomorrow"
>> timeago(Date.today.next_day(7))
=> "in 1 week"
>> timeago(Date.today.next_day(1000))
=> "in 2 years and 8 months"
NOTE If you have the gem installed in your system globally, and you're not using Bundler (probably because you're are writing a basic script), don't forget to require the library first:
require 'jekyll-timeago'
include Jekyll::Timeago
puts timeago('2030-1-1')
locale
To use a different language:
>> timeago(Date.today.prev_day(200), locale: :es)
=> "hace 6 meses y 2 semanas"
>> timeago(Date.today.prev_day(200), locale: :fr)
=> "il y a environ 6 mois et 2 semaines"
Read more about the localization options here.
depth
You are able to change the level of detail (from 1 up to 4, 2 by default) to get higher or lower granularity:
>> timeago(Date.today.prev_day(2000), depth: 3)
=> "5 years, 5 months and 3 weeks ago"
>> timeago(Date.today.prev_day(2000), depth: 4)
=> "5 years, 5 months, 3 weeks and 4 days ago"
threshold
The next component in the time must at least match this threshold to be picked. Set to 0 by default, so you don't get any approximations. Can be used to drop "straggling" values which are too low to be of any use (in 7 months and 2 days
is as good as saying in 7 months
).
>> timeago(Date.today.prev_day(366), depth: 2, threshold: 0.05)
=> "1 year ago"
By default, jekyll-timego
already provides translations for some languages. You can check the list here. However, you are able to provide your own translations, or even override the originals, easily.
This project uses the mini_i18n gem under the hood to deal with translations. You can read further about all options in its docs. Example:
MiniI18n.configure do |config|
config.load_translations('/path_to_your_translations_files/*.yml')
config.default_locale = :es
end
If you want to contribute and support more default languages, please feel free to send a pull request.
You can also use jekyll-timeago
from the command line:
> jekyll-timeago --help
> jekyll-timeago 2016-1-1
2 years and 6 months ago
> jekyll-timeago 2016-1-1 --locale fr
il y a environ 2 années et 6 mois
Starts a custom IRB session with the timeago
method included:
> jekyll-timeago --console
>> timeago(Date.today)
=> "today"
You have different options to install and use jekyll-timeago
into your Jekyll project:
Install the gem
to your system:
> gem install jekyll-timeago
In your _config.yml
file, add a new array with the key gems and the values of the gem names of the plugins you’d like to use. In this case:
plugins:
- jekyll-timeago
Add this gem to your Gemfile
and run bundle install
:
group :jekyll_plugins do
gem 'jekyll-timeago'
end
Liquid Filter:
<p>{{ page.date | timeago }}</p>
<p>{{ page.date | timeago: '2020-1-1' }}</p>
Liquid Tag:
<p>{% timeago 2000-1-1 %}</p>
<p>{% timeago 2000-1-1 2010-1-1 %}</p>
In your _config.yml
file, you can customize the following options:
jekyll_timeago:
depth: 2
translations_path: '/path_to_your_translations/*.yaml'
default_locale: 'en'
fallbacks: true
available_locales:
- 'en'
- 'es'
- 'fr'
Also, you can set a different language per page using the Front Matter functionality:
---
locale: 'es'
---
Any kind of feedback, bug report, idea or enhancement are really appreciated.
To contribute, just fork the repo, hack on it and send a pull request. Don't forget to add specs for behaviour changes and run the test suite:
> bundle exec rake
Copyright (c) Marc Anguera. Jekyll-Timeago is released under the MIT License.
1.问题描述 运行jekyll server时报错: Current Bundler version: bundler (1.11.2) This Gemfile requires a different version of Bundler. Perhaps you need to update Bundler by running `gem install bundler`? Cou
这个问题好像只有在国内才会出现,就是在安装完ruby后,运行 gem install jekyll时会报 ERROR: Could not find a valid gem 'jekyll' (>= 0) in any repository ERROR: Possible alternatives: jekyll` 的错误,原因是rubygems.org被block了。 解决办法是使用国内
使用命令:gem install jekyll gem install jekyll ERROR: Could not find a valid gem 'jekyll' (>= 0), here is why: Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=
Jekyll 是一个简单的免费的Blog生成工具,类似WordPress。但是和WordPress又有很大的不同,原因是jekyll只是一个生成静态网页的工具,不需要数据库支持。但是可以配合第三方服务,例如discuz。最关键的是jekyll可以免费部署在Github上,而且可以绑定自己的域名。 快速安装指令: gem install jekylljekyll new my-awesome-sit
中文网站 jekyllcn 快速开始 ~ $ gem install jekyll bundler ~ $ jekyll new my-awesome-site ~ $ cd my-awesome-site ~/my-awesome-site $ bundle install ~/my-awesome-site $ bundle exec jekyll serve # => 打开浏览器 http
jekyll-admin 是一个 jekyll 插件,为用户提供了传统 CMS(内容管理系统)风格的图形化界面来创作内容和管理 jekyll 网站。 该项目分为两部分。基于 Ruby 的 HTTP API 处理 jekyll 和文件系统的操作部分,以及在这个 API 基础上的基于 JavaScript 的前端部分。 安装: 就像安装其他插件一样,请参阅 jekyll 文档的插件安装部分安装 jek
Jekyll的增强版,使用Markdown来写日志。 Jekyll采用静态文件方式管理,不需要数据库即可支持一个独立博客站点,在github-pages平台上被普遍采用。Jekyll-Bootstrap在Jekyll基础上,集成了twitter-bootstrap界面风格和一些实用的插件,并且易于扩展。
Jekyll Docker Jekyll Docker is a software image that has Jekyll and many of its dependencies ready to use for you in an encapsulated format. It includes a default set of gems, different image types wi
jekyll-katex This is a Jekyll plugin for performing compile-time math rendering via the KaTeX library.KaTeX is a library for rending math on the web using LaTeX, similar to MathJax. KaTeX differs from