[英]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. 我写了一个名为moment的库,它执行DateJS所做的事情,只是它更小,不修改Date.prototype
,并且在浏览器和Date.prototype
中都有效。
npm install moment
Usage: 用法:
moment(1316369911638).fromNow() // "3 minutes ago"
It supports i18n and customization as well, all strings are exposed for modification. 它也支持i18n和自定义,所有字符串都可以进行修改。
Something you can try is date.js: http://www.datejs.com/ 你可以尝试的东西是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. 假设date.js在同一文件夹中,否则修改require路径。
Then a simple code sample to test is: 然后一个简单的代码示例进行测试:
console.log( date.today().next().thursday() )
I've found that require(./date)
(ie using datejs directly) and datejs from npm install datejs
don't work as advertised, at least with node v0.4.9. 我发现require(./date)
(即直接使用datejs)和来自npm install datejs
不能像宣传的那样工作,至少对于节点v0.4.9。
The datetime
module seems to work for me though: datetime
模块似乎对我有用:
$ 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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.