Javascript library for human-friendly relative dat

2019-01-06 15:22发布

I'd like to display some dates as relative to the current date in a human-friendly format.

Examples of human-friendly relative dates:

  • 10 seconds ago
  • 20 minutes from now
  • 1 day ago
  • 5 weeks ago
  • 2 months ago

Basically faithfully preserving the highest order of magnitude (and by preference, only shifting up units when passing 2 of those units - 5 weeks instead of 1 month).

Though I could live with a library that had less control and even more friendly dates like:

  • yesterday
  • tomorrow
  • last week
  • a few minutes ago
  • in a couple hours

Any popular libraries for this?

7条回答
聊天终结者
2楼-- · 2019-01-06 15:51

here an example of sugar vs moment: for a calendar that displays weeks, I needed the last monday value:

moment.js

var m = moment().subtract("days", 1).sod().day(1) // returns a "moment"

sugar.js

var d = Date.past("monday") // returns a js Date object

I much prefer sugar and after some months with moment.js now switch to sugar.js. it is clearer and integrates nicely with Javascripts' Date class.

OP cases are covered by both libs, for sugar.js see http://sugarjs.com/dates

查看更多
登录 后发表回答