Are there any Node.js modules that provide fuzzy date strings?
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 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.com/
To make it node compatible 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
datetime@0.0.2 ./node_modules/datetime
└── vows@0.5.8
$ 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'
精彩评论