Search code examples
node.jsexpressdatejs

Usage Examples of DateJS in NodeJS


I have installed DateJS in my NodeJS project for easy handling of adding months. I added it in my package.json.

 "datejs": "*"

Then installed it via :

root@v-hj-0190:~/deepak/appJade# npm -d install
npm info it worked if it ends with ok
npm info using npm@1.4.28
npm info using node@v0.10.33
npm info preinstall appJade@0.0.0
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 datejs@1.0.0-rc3 into /root/deepak/appJade
npm info installOne datejs@1.0.0-rc3
npm info preinstall datejs@1.0.0-rc3
npm info build /root/deepak/appJade/node_modules/datejs
npm info linkStuff datejs@1.0.0-rc3
npm info install datejs@1.0.0-rc3
npm info postinstall datejs@1.0.0-rc3
npm info build /root/deepak/appJade
npm info linkStuff appJade@0.0.0
npm info install appJade@0.0.0
npm info postinstall appJade@0.0.0
npm info prepublish appJade@0.0.0
datejs@1.0.0-rc3 node_modules/datejs
npm info ok
root@v-hj-0190:~/deepak/appJade#  

I have added the require line in app.js

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

But the following codes are still giving error:

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'

Note: I am new to NodeJS and need examples of how to integrate/use DateJS in a project.Only direct functions have been provided everywhere.


Solution

  • datejs works by extending the javascript built in Date, so no need to assign the require to a variable. Instead of..

    var Date = require('datejs');
    

    just do the require:

    require('datejs')
    

    and then when you will be all set (alternatively, if you have some reason to assign the module to a variable, use something other than 'Date').