2015-02-06 52 views
3

我已經在NodeJS項目中安裝了DateJS,以便輕鬆處理添加月份。我將它添加到我的package.json中。NodeJS中DateJS的使用示例

"datejs": "*" 

然後通過安裝了它:

[email protected]:~/deepak/appJade# npm -d install 
npm info it worked if it ends with ok 
npm info using [email protected] 
npm info using [email protected] 
npm info preinstall [email protected] 
npm info trying registry request attempt 1 at 15:08:34 
npm http GET https://registry.npmjs.org/datejs 
npm http 304 https://registry.npmjs.org/datejs 
npm info install [email protected] into /root/deepak/appJade 
npm info installOne [email protected] 
npm info preinstall [email protected] 
npm info build /root/deepak/appJade/node_modules/datejs 
npm info linkStuff [email protected] 
npm info install [email protected] 
npm info postinstall [email protected] 
npm info build /root/deepak/appJade 
npm info linkStuff [email protected] 
npm info install [email protected] 
npm info postinstall [email protected] 
npm info prepublish [email protected] 
[email protected] node_modules/datejs 
npm info ok 
[email protected]:~/deepak/appJade# 

我已經添加在app.js

... 
var bodyParser = require('body-parser'); 
var Date = require('datejs'); 
... 

的要求線,但下面的代碼仍然給錯誤:

var approvalDate = Date.today(); 
           ^
TypeError: Object function Date() { [native code] } has no method 'today' 
.. 


var n = 6; 
console.log(n.months().fromNow()); 
        ^
TypeError: Object 6 has no method 'months' 

注意:我是NodeJS的新手,需要如何在一個項目中集成/使用DateJS的例子。只有直接的功能被提供到處。

回答

10

datejs通過擴展在Date中構建的javascript工作,所以不需要將require分配給一個變量。取而代之的..

var Date = require('datejs'); 

只是做了要求:

require('datejs') 

,然後當你將所有設置(或者,如果你有一些理由的模塊分配給一個變量,使用其他的東西比'日期')。

+0

感謝邁克那工作..只有懷疑這個擴展日期對象工作與貓鼬n mongodb查詢 – dv3 2015-02-09 10:41:41