Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 [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
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.

like image 349
dv3 Avatar asked Dec 15 '22 16:12

dv3


1 Answers

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').

like image 181
Mike Atkins Avatar answered Jan 03 '23 23:01

Mike Atkins