最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Are there any Node.js modules that provide fuzzy date strings? - Stack Overflow

programmeradmin0浏览0评论

I'm thinking of strings like "one minute ago" or "3 weeks ago", that kind of thing.

I could easily port examples I've found in other languages but there's no need to reinvent the wheel if this stuff is already out there.

I'm thinking of strings like "one minute ago" or "3 weeks ago", that kind of thing.

I could easily port examples I've found in other languages but there's no need to reinvent the wheel if this stuff is already out there.

Share Improve this question edited May 22, 2011 at 22:02 alnorth29 asked May 22, 2011 at 17:49 alnorth29alnorth29 3,6022 gold badges35 silver badges51 bronze badges 1
  • This might help. Shows you a fuzzy date based on the current time. – Raynos Commented May 22, 2011 at 17:55
Add a ment  | 

3 Answers 3

Reset to default 5

I wrote a library called moment that does what DateJS does, only it's smaller, doesn't modify Date.prototype, and works in both the browser and NodeJS.

npm install moment

Usage:

moment(1316369911638).fromNow() // "3 minutes ago"

It supports i18n and customization as well, all strings are exposed for modification.

Something you can try is date.js: http://www.datejs./

To make it node patible at the very bottom of the script add the line:

module.exports = Date;

Then you can require it:

var date = require('./date');

Assuming date.js is in the same folder, otherwise modify the require path.

Then a simple code sample to test is:

console.log( date.today().next().thursday() )

I've found that require(./date) (i.e. using datejs directly) and datejs from npm install datejs don't work as advertised, at least with node v0.4.9.

The datetime module seems to work for me though:

$ npm install datetime
[email protected] ./node_modules/datetime 
└── [email protected]
$ node --version
v0.4.9
$ node
> var datetime = require('datetime')
> now = new Date()
Thu, 14 Jul 2011 05:50:06 GMT
> # wait for a bit
... 
... 
> datetime.formatAgo(now)
'18 seconds ago'
发布评论

评论列表(0)

  1. 暂无评论